request.c 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * BSD LICENSE
  25. *
  26. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  27. * All rights reserved.
  28. *
  29. * Redistribution and use in source and binary forms, with or without
  30. * modification, are permitted provided that the following conditions
  31. * are met:
  32. *
  33. * * Redistributions of source code must retain the above copyright
  34. * notice, this list of conditions and the following disclaimer.
  35. * * Redistributions in binary form must reproduce the above copyright
  36. * notice, this list of conditions and the following disclaimer in
  37. * the documentation and/or other materials provided with the
  38. * distribution.
  39. * * Neither the name of Intel Corporation nor the names of its
  40. * contributors may be used to endorse or promote products derived
  41. * from this software without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  44. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  45. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  46. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  47. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  48. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  49. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  50. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  51. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  53. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. */
  55. #include "isci.h"
  56. #include "task.h"
  57. #include "request.h"
  58. #include "sata.h"
  59. #include "scu_completion_codes.h"
  60. #include "scu_event_codes.h"
  61. #include "sas.h"
  62. /**
  63. * This method returns the sgl element pair for the specificed sgl_pair index.
  64. * @sci_req: This parameter specifies the IO request for which to retrieve
  65. * the Scatter-Gather List element pair.
  66. * @sgl_pair_index: This parameter specifies the index into the SGL element
  67. * pair to be retrieved.
  68. *
  69. * This method returns a pointer to an struct scu_sgl_element_pair.
  70. */
  71. static struct scu_sgl_element_pair *scic_sds_request_get_sgl_element_pair(
  72. struct scic_sds_request *sci_req,
  73. u32 sgl_pair_index
  74. ) {
  75. struct scu_task_context *task_context;
  76. task_context = (struct scu_task_context *)sci_req->task_context_buffer;
  77. if (sgl_pair_index == 0) {
  78. return &task_context->sgl_pair_ab;
  79. } else if (sgl_pair_index == 1) {
  80. return &task_context->sgl_pair_cd;
  81. }
  82. return &sci_req->sg_table[sgl_pair_index - 2];
  83. }
  84. /**
  85. * This function will build the SGL list for an IO request.
  86. * @sci_req: This parameter specifies the IO request for which to build
  87. * the Scatter-Gather List.
  88. *
  89. */
  90. static void scic_sds_request_build_sgl(struct scic_sds_request *sds_request)
  91. {
  92. struct isci_request *isci_request = sci_req_to_ireq(sds_request);
  93. struct isci_host *isci_host = isci_request->isci_host;
  94. struct sas_task *task = isci_request_access_task(isci_request);
  95. struct scatterlist *sg = NULL;
  96. dma_addr_t dma_addr;
  97. u32 sg_idx = 0;
  98. struct scu_sgl_element_pair *scu_sg = NULL;
  99. struct scu_sgl_element_pair *prev_sg = NULL;
  100. if (task->num_scatter > 0) {
  101. sg = task->scatter;
  102. while (sg) {
  103. scu_sg = scic_sds_request_get_sgl_element_pair(
  104. sds_request,
  105. sg_idx);
  106. SCU_SGL_COPY(scu_sg->A, sg);
  107. sg = sg_next(sg);
  108. if (sg) {
  109. SCU_SGL_COPY(scu_sg->B, sg);
  110. sg = sg_next(sg);
  111. } else
  112. SCU_SGL_ZERO(scu_sg->B);
  113. if (prev_sg) {
  114. dma_addr =
  115. scic_io_request_get_dma_addr(
  116. sds_request,
  117. scu_sg);
  118. prev_sg->next_pair_upper =
  119. upper_32_bits(dma_addr);
  120. prev_sg->next_pair_lower =
  121. lower_32_bits(dma_addr);
  122. }
  123. prev_sg = scu_sg;
  124. sg_idx++;
  125. }
  126. } else { /* handle when no sg */
  127. scu_sg = scic_sds_request_get_sgl_element_pair(sds_request,
  128. sg_idx);
  129. dma_addr = dma_map_single(&isci_host->pdev->dev,
  130. task->scatter,
  131. task->total_xfer_len,
  132. task->data_dir);
  133. isci_request->zero_scatter_daddr = dma_addr;
  134. scu_sg->A.length = task->total_xfer_len;
  135. scu_sg->A.address_upper = upper_32_bits(dma_addr);
  136. scu_sg->A.address_lower = lower_32_bits(dma_addr);
  137. }
  138. if (scu_sg) {
  139. scu_sg->next_pair_upper = 0;
  140. scu_sg->next_pair_lower = 0;
  141. }
  142. }
  143. static void scic_sds_io_request_build_ssp_command_iu(struct scic_sds_request *sci_req)
  144. {
  145. struct ssp_cmd_iu *cmd_iu;
  146. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  147. struct sas_task *task = isci_request_access_task(ireq);
  148. cmd_iu = &sci_req->ssp.cmd;
  149. memcpy(cmd_iu->LUN, task->ssp_task.LUN, 8);
  150. cmd_iu->add_cdb_len = 0;
  151. cmd_iu->_r_a = 0;
  152. cmd_iu->_r_b = 0;
  153. cmd_iu->en_fburst = 0; /* unsupported */
  154. cmd_iu->task_prio = task->ssp_task.task_prio;
  155. cmd_iu->task_attr = task->ssp_task.task_attr;
  156. cmd_iu->_r_c = 0;
  157. sci_swab32_cpy(&cmd_iu->cdb, task->ssp_task.cdb,
  158. sizeof(task->ssp_task.cdb) / sizeof(u32));
  159. }
  160. static void scic_sds_task_request_build_ssp_task_iu(struct scic_sds_request *sci_req)
  161. {
  162. struct ssp_task_iu *task_iu;
  163. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  164. struct sas_task *task = isci_request_access_task(ireq);
  165. struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
  166. task_iu = &sci_req->ssp.tmf;
  167. memset(task_iu, 0, sizeof(struct ssp_task_iu));
  168. memcpy(task_iu->LUN, task->ssp_task.LUN, 8);
  169. task_iu->task_func = isci_tmf->tmf_code;
  170. task_iu->task_tag =
  171. (ireq->ttype == tmf_task) ?
  172. isci_tmf->io_tag :
  173. SCI_CONTROLLER_INVALID_IO_TAG;
  174. }
  175. /**
  176. * This method is will fill in the SCU Task Context for any type of SSP request.
  177. * @sci_req:
  178. * @task_context:
  179. *
  180. */
  181. static void scu_ssp_reqeust_construct_task_context(
  182. struct scic_sds_request *sds_request,
  183. struct scu_task_context *task_context)
  184. {
  185. dma_addr_t dma_addr;
  186. struct scic_sds_controller *controller;
  187. struct scic_sds_remote_device *target_device;
  188. struct scic_sds_port *target_port;
  189. controller = scic_sds_request_get_controller(sds_request);
  190. target_device = scic_sds_request_get_device(sds_request);
  191. target_port = scic_sds_request_get_port(sds_request);
  192. /* Fill in the TC with the its required data */
  193. task_context->abort = 0;
  194. task_context->priority = 0;
  195. task_context->initiator_request = 1;
  196. task_context->connection_rate = target_device->connection_rate;
  197. task_context->protocol_engine_index =
  198. scic_sds_controller_get_protocol_engine_group(controller);
  199. task_context->logical_port_index =
  200. scic_sds_port_get_index(target_port);
  201. task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
  202. task_context->valid = SCU_TASK_CONTEXT_VALID;
  203. task_context->context_type = SCU_TASK_CONTEXT_TYPE;
  204. task_context->remote_node_index =
  205. scic_sds_remote_device_get_index(sds_request->target_device);
  206. task_context->command_code = 0;
  207. task_context->link_layer_control = 0;
  208. task_context->do_not_dma_ssp_good_response = 1;
  209. task_context->strict_ordering = 0;
  210. task_context->control_frame = 0;
  211. task_context->timeout_enable = 0;
  212. task_context->block_guard_enable = 0;
  213. task_context->address_modifier = 0;
  214. /* task_context->type.ssp.tag = sci_req->io_tag; */
  215. task_context->task_phase = 0x01;
  216. if (sds_request->was_tag_assigned_by_user) {
  217. /*
  218. * Build the task context now since we have already read
  219. * the data
  220. */
  221. sds_request->post_context =
  222. (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  223. (scic_sds_controller_get_protocol_engine_group(
  224. controller) <<
  225. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  226. (scic_sds_port_get_index(target_port) <<
  227. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
  228. scic_sds_io_tag_get_index(sds_request->io_tag));
  229. } else {
  230. /*
  231. * Build the task context now since we have already read
  232. * the data
  233. *
  234. * I/O tag index is not assigned because we have to wait
  235. * until we get a TCi
  236. */
  237. sds_request->post_context =
  238. (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  239. (scic_sds_controller_get_protocol_engine_group(
  240. owning_controller) <<
  241. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  242. (scic_sds_port_get_index(target_port) <<
  243. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
  244. }
  245. /*
  246. * Copy the physical address for the command buffer to the
  247. * SCU Task Context
  248. */
  249. dma_addr = scic_io_request_get_dma_addr(sds_request,
  250. &sds_request->ssp.cmd);
  251. task_context->command_iu_upper = upper_32_bits(dma_addr);
  252. task_context->command_iu_lower = lower_32_bits(dma_addr);
  253. /*
  254. * Copy the physical address for the response buffer to the
  255. * SCU Task Context
  256. */
  257. dma_addr = scic_io_request_get_dma_addr(sds_request,
  258. &sds_request->ssp.rsp);
  259. task_context->response_iu_upper = upper_32_bits(dma_addr);
  260. task_context->response_iu_lower = lower_32_bits(dma_addr);
  261. }
  262. /**
  263. * This method is will fill in the SCU Task Context for a SSP IO request.
  264. * @sci_req:
  265. *
  266. */
  267. static void scu_ssp_io_request_construct_task_context(
  268. struct scic_sds_request *sci_req,
  269. enum dma_data_direction dir,
  270. u32 len)
  271. {
  272. struct scu_task_context *task_context;
  273. task_context = scic_sds_request_get_task_context(sci_req);
  274. scu_ssp_reqeust_construct_task_context(sci_req, task_context);
  275. task_context->ssp_command_iu_length =
  276. sizeof(struct ssp_cmd_iu) / sizeof(u32);
  277. task_context->type.ssp.frame_type = SSP_COMMAND;
  278. switch (dir) {
  279. case DMA_FROM_DEVICE:
  280. case DMA_NONE:
  281. default:
  282. task_context->task_type = SCU_TASK_TYPE_IOREAD;
  283. break;
  284. case DMA_TO_DEVICE:
  285. task_context->task_type = SCU_TASK_TYPE_IOWRITE;
  286. break;
  287. }
  288. task_context->transfer_length_bytes = len;
  289. if (task_context->transfer_length_bytes > 0)
  290. scic_sds_request_build_sgl(sci_req);
  291. }
  292. /**
  293. * This method will fill in the SCU Task Context for a SSP Task request. The
  294. * following important settings are utilized: -# priority ==
  295. * SCU_TASK_PRIORITY_HIGH. This ensures that the task request is issued
  296. * ahead of other task destined for the same Remote Node. -# task_type ==
  297. * SCU_TASK_TYPE_IOREAD. This simply indicates that a normal request type
  298. * (i.e. non-raw frame) is being utilized to perform task management. -#
  299. * control_frame == 1. This ensures that the proper endianess is set so
  300. * that the bytes are transmitted in the right order for a task frame.
  301. * @sci_req: This parameter specifies the task request object being
  302. * constructed.
  303. *
  304. */
  305. static void scu_ssp_task_request_construct_task_context(
  306. struct scic_sds_request *sci_req)
  307. {
  308. struct scu_task_context *task_context;
  309. task_context = scic_sds_request_get_task_context(sci_req);
  310. scu_ssp_reqeust_construct_task_context(sci_req, task_context);
  311. task_context->control_frame = 1;
  312. task_context->priority = SCU_TASK_PRIORITY_HIGH;
  313. task_context->task_type = SCU_TASK_TYPE_RAW_FRAME;
  314. task_context->transfer_length_bytes = 0;
  315. task_context->type.ssp.frame_type = SSP_TASK;
  316. task_context->ssp_command_iu_length =
  317. sizeof(struct ssp_task_iu) / sizeof(u32);
  318. }
  319. /**
  320. * This method is will fill in the SCU Task Context for any type of SATA
  321. * request. This is called from the various SATA constructors.
  322. * @sci_req: The general IO request object which is to be used in
  323. * constructing the SCU task context.
  324. * @task_context: The buffer pointer for the SCU task context which is being
  325. * constructed.
  326. *
  327. * The general io request construction is complete. The buffer assignment for
  328. * the command buffer is complete. none Revisit task context construction to
  329. * determine what is common for SSP/SMP/STP task context structures.
  330. */
  331. static void scu_sata_reqeust_construct_task_context(
  332. struct scic_sds_request *sci_req,
  333. struct scu_task_context *task_context)
  334. {
  335. dma_addr_t dma_addr;
  336. struct scic_sds_controller *controller;
  337. struct scic_sds_remote_device *target_device;
  338. struct scic_sds_port *target_port;
  339. controller = scic_sds_request_get_controller(sci_req);
  340. target_device = scic_sds_request_get_device(sci_req);
  341. target_port = scic_sds_request_get_port(sci_req);
  342. /* Fill in the TC with the its required data */
  343. task_context->abort = 0;
  344. task_context->priority = SCU_TASK_PRIORITY_NORMAL;
  345. task_context->initiator_request = 1;
  346. task_context->connection_rate = target_device->connection_rate;
  347. task_context->protocol_engine_index =
  348. scic_sds_controller_get_protocol_engine_group(controller);
  349. task_context->logical_port_index =
  350. scic_sds_port_get_index(target_port);
  351. task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_STP;
  352. task_context->valid = SCU_TASK_CONTEXT_VALID;
  353. task_context->context_type = SCU_TASK_CONTEXT_TYPE;
  354. task_context->remote_node_index =
  355. scic_sds_remote_device_get_index(sci_req->target_device);
  356. task_context->command_code = 0;
  357. task_context->link_layer_control = 0;
  358. task_context->do_not_dma_ssp_good_response = 1;
  359. task_context->strict_ordering = 0;
  360. task_context->control_frame = 0;
  361. task_context->timeout_enable = 0;
  362. task_context->block_guard_enable = 0;
  363. task_context->address_modifier = 0;
  364. task_context->task_phase = 0x01;
  365. task_context->ssp_command_iu_length =
  366. (sizeof(struct host_to_dev_fis) - sizeof(u32)) / sizeof(u32);
  367. /* Set the first word of the H2D REG FIS */
  368. task_context->type.words[0] = *(u32 *)&sci_req->stp.cmd;
  369. if (sci_req->was_tag_assigned_by_user) {
  370. /*
  371. * Build the task context now since we have already read
  372. * the data
  373. */
  374. sci_req->post_context =
  375. (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  376. (scic_sds_controller_get_protocol_engine_group(
  377. controller) <<
  378. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  379. (scic_sds_port_get_index(target_port) <<
  380. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
  381. scic_sds_io_tag_get_index(sci_req->io_tag));
  382. } else {
  383. /*
  384. * Build the task context now since we have already read
  385. * the data.
  386. * I/O tag index is not assigned because we have to wait
  387. * until we get a TCi.
  388. */
  389. sci_req->post_context =
  390. (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  391. (scic_sds_controller_get_protocol_engine_group(
  392. controller) <<
  393. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  394. (scic_sds_port_get_index(target_port) <<
  395. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
  396. }
  397. /*
  398. * Copy the physical address for the command buffer to the SCU Task
  399. * Context. We must offset the command buffer by 4 bytes because the
  400. * first 4 bytes are transfered in the body of the TC.
  401. */
  402. dma_addr = scic_io_request_get_dma_addr(sci_req,
  403. ((char *) &sci_req->stp.cmd) +
  404. sizeof(u32));
  405. task_context->command_iu_upper = upper_32_bits(dma_addr);
  406. task_context->command_iu_lower = lower_32_bits(dma_addr);
  407. /* SATA Requests do not have a response buffer */
  408. task_context->response_iu_upper = 0;
  409. task_context->response_iu_lower = 0;
  410. }
  411. /**
  412. * scu_stp_raw_request_construct_task_context -
  413. * @sci_req: This parameter specifies the STP request object for which to
  414. * construct a RAW command frame task context.
  415. * @task_context: This parameter specifies the SCU specific task context buffer
  416. * to construct.
  417. *
  418. * This method performs the operations common to all SATA/STP requests
  419. * utilizing the raw frame method. none
  420. */
  421. static void scu_stp_raw_request_construct_task_context(struct scic_sds_stp_request *stp_req,
  422. struct scu_task_context *task_context)
  423. {
  424. struct scic_sds_request *sci_req = to_sci_req(stp_req);
  425. scu_sata_reqeust_construct_task_context(sci_req, task_context);
  426. task_context->control_frame = 0;
  427. task_context->priority = SCU_TASK_PRIORITY_NORMAL;
  428. task_context->task_type = SCU_TASK_TYPE_SATA_RAW_FRAME;
  429. task_context->type.stp.fis_type = FIS_REGH2D;
  430. task_context->transfer_length_bytes = sizeof(struct host_to_dev_fis) - sizeof(u32);
  431. }
  432. static enum sci_status
  433. scic_sds_stp_pio_request_construct(struct scic_sds_request *sci_req,
  434. bool copy_rx_frame)
  435. {
  436. struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
  437. struct scic_sds_stp_pio_request *pio = &stp_req->type.pio;
  438. scu_stp_raw_request_construct_task_context(stp_req,
  439. sci_req->task_context_buffer);
  440. pio->current_transfer_bytes = 0;
  441. pio->ending_error = 0;
  442. pio->ending_status = 0;
  443. pio->request_current.sgl_offset = 0;
  444. pio->request_current.sgl_set = SCU_SGL_ELEMENT_PAIR_A;
  445. if (copy_rx_frame) {
  446. scic_sds_request_build_sgl(sci_req);
  447. /* Since the IO request copy of the TC contains the same data as
  448. * the actual TC this pointer is vaild for either.
  449. */
  450. pio->request_current.sgl_pair = &sci_req->task_context_buffer->sgl_pair_ab;
  451. } else {
  452. /* The user does not want the data copied to the SGL buffer location */
  453. pio->request_current.sgl_pair = NULL;
  454. }
  455. return SCI_SUCCESS;
  456. }
  457. /**
  458. *
  459. * @sci_req: This parameter specifies the request to be constructed as an
  460. * optimized request.
  461. * @optimized_task_type: This parameter specifies whether the request is to be
  462. * an UDMA request or a NCQ request. - A value of 0 indicates UDMA. - A
  463. * value of 1 indicates NCQ.
  464. *
  465. * This method will perform request construction common to all types of STP
  466. * requests that are optimized by the silicon (i.e. UDMA, NCQ). This method
  467. * returns an indication as to whether the construction was successful.
  468. */
  469. static void scic_sds_stp_optimized_request_construct(struct scic_sds_request *sci_req,
  470. u8 optimized_task_type,
  471. u32 len,
  472. enum dma_data_direction dir)
  473. {
  474. struct scu_task_context *task_context = sci_req->task_context_buffer;
  475. /* Build the STP task context structure */
  476. scu_sata_reqeust_construct_task_context(sci_req, task_context);
  477. /* Copy over the SGL elements */
  478. scic_sds_request_build_sgl(sci_req);
  479. /* Copy over the number of bytes to be transfered */
  480. task_context->transfer_length_bytes = len;
  481. if (dir == DMA_TO_DEVICE) {
  482. /*
  483. * The difference between the DMA IN and DMA OUT request task type
  484. * values are consistent with the difference between FPDMA READ
  485. * and FPDMA WRITE values. Add the supplied task type parameter
  486. * to this difference to set the task type properly for this
  487. * DATA OUT (WRITE) case. */
  488. task_context->task_type = optimized_task_type + (SCU_TASK_TYPE_DMA_OUT
  489. - SCU_TASK_TYPE_DMA_IN);
  490. } else {
  491. /*
  492. * For the DATA IN (READ) case, simply save the supplied
  493. * optimized task type. */
  494. task_context->task_type = optimized_task_type;
  495. }
  496. }
  497. static enum sci_status
  498. scic_io_request_construct_sata(struct scic_sds_request *sci_req,
  499. u32 len,
  500. enum dma_data_direction dir,
  501. bool copy)
  502. {
  503. enum sci_status status = SCI_SUCCESS;
  504. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  505. struct sas_task *task = isci_request_access_task(ireq);
  506. /* check for management protocols */
  507. if (ireq->ttype == tmf_task) {
  508. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  509. if (tmf->tmf_code == isci_tmf_sata_srst_high ||
  510. tmf->tmf_code == isci_tmf_sata_srst_low) {
  511. scu_stp_raw_request_construct_task_context(&sci_req->stp.req,
  512. sci_req->task_context_buffer);
  513. return SCI_SUCCESS;
  514. } else {
  515. dev_err(scic_to_dev(sci_req->owning_controller),
  516. "%s: Request 0x%p received un-handled SAT "
  517. "management protocol 0x%x.\n",
  518. __func__, sci_req, tmf->tmf_code);
  519. return SCI_FAILURE;
  520. }
  521. }
  522. if (!sas_protocol_ata(task->task_proto)) {
  523. dev_err(scic_to_dev(sci_req->owning_controller),
  524. "%s: Non-ATA protocol in SATA path: 0x%x\n",
  525. __func__,
  526. task->task_proto);
  527. return SCI_FAILURE;
  528. }
  529. /* non data */
  530. if (task->data_dir == DMA_NONE) {
  531. scu_stp_raw_request_construct_task_context(&sci_req->stp.req,
  532. sci_req->task_context_buffer);
  533. return SCI_SUCCESS;
  534. }
  535. /* NCQ */
  536. if (task->ata_task.use_ncq) {
  537. scic_sds_stp_optimized_request_construct(sci_req,
  538. SCU_TASK_TYPE_FPDMAQ_READ,
  539. len, dir);
  540. return SCI_SUCCESS;
  541. }
  542. /* DMA */
  543. if (task->ata_task.dma_xfer) {
  544. scic_sds_stp_optimized_request_construct(sci_req,
  545. SCU_TASK_TYPE_DMA_IN,
  546. len, dir);
  547. return SCI_SUCCESS;
  548. } else /* PIO */
  549. return scic_sds_stp_pio_request_construct(sci_req, copy);
  550. return status;
  551. }
  552. static enum sci_status scic_io_request_construct_basic_ssp(struct scic_sds_request *sci_req)
  553. {
  554. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  555. struct sas_task *task = isci_request_access_task(ireq);
  556. sci_req->protocol = SCIC_SSP_PROTOCOL;
  557. scu_ssp_io_request_construct_task_context(sci_req,
  558. task->data_dir,
  559. task->total_xfer_len);
  560. scic_sds_io_request_build_ssp_command_iu(sci_req);
  561. sci_base_state_machine_change_state(&sci_req->state_machine,
  562. SCI_BASE_REQUEST_STATE_CONSTRUCTED);
  563. return SCI_SUCCESS;
  564. }
  565. enum sci_status scic_task_request_construct_ssp(
  566. struct scic_sds_request *sci_req)
  567. {
  568. /* Construct the SSP Task SCU Task Context */
  569. scu_ssp_task_request_construct_task_context(sci_req);
  570. /* Fill in the SSP Task IU */
  571. scic_sds_task_request_build_ssp_task_iu(sci_req);
  572. sci_base_state_machine_change_state(&sci_req->state_machine,
  573. SCI_BASE_REQUEST_STATE_CONSTRUCTED);
  574. return SCI_SUCCESS;
  575. }
  576. static enum sci_status scic_io_request_construct_basic_sata(struct scic_sds_request *sci_req)
  577. {
  578. enum sci_status status;
  579. struct scic_sds_stp_request *stp_req;
  580. bool copy = false;
  581. struct isci_request *isci_request = sci_req_to_ireq(sci_req);
  582. struct sas_task *task = isci_request_access_task(isci_request);
  583. stp_req = &sci_req->stp.req;
  584. sci_req->protocol = SCIC_STP_PROTOCOL;
  585. copy = (task->data_dir == DMA_NONE) ? false : true;
  586. status = scic_io_request_construct_sata(sci_req,
  587. task->total_xfer_len,
  588. task->data_dir,
  589. copy);
  590. if (status == SCI_SUCCESS)
  591. sci_base_state_machine_change_state(&sci_req->state_machine,
  592. SCI_BASE_REQUEST_STATE_CONSTRUCTED);
  593. return status;
  594. }
  595. enum sci_status scic_task_request_construct_sata(struct scic_sds_request *sci_req)
  596. {
  597. enum sci_status status = SCI_SUCCESS;
  598. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  599. /* check for management protocols */
  600. if (ireq->ttype == tmf_task) {
  601. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  602. if (tmf->tmf_code == isci_tmf_sata_srst_high ||
  603. tmf->tmf_code == isci_tmf_sata_srst_low) {
  604. scu_stp_raw_request_construct_task_context(&sci_req->stp.req,
  605. sci_req->task_context_buffer);
  606. } else {
  607. dev_err(scic_to_dev(sci_req->owning_controller),
  608. "%s: Request 0x%p received un-handled SAT "
  609. "Protocol 0x%x.\n",
  610. __func__, sci_req, tmf->tmf_code);
  611. return SCI_FAILURE;
  612. }
  613. }
  614. if (status != SCI_SUCCESS)
  615. return status;
  616. sci_base_state_machine_change_state(&sci_req->state_machine,
  617. SCI_BASE_REQUEST_STATE_CONSTRUCTED);
  618. return status;
  619. }
  620. /**
  621. * sci_req_tx_bytes - bytes transferred when reply underruns request
  622. * @sci_req: request that was terminated early
  623. */
  624. #define SCU_TASK_CONTEXT_SRAM 0x200000
  625. static u32 sci_req_tx_bytes(struct scic_sds_request *sci_req)
  626. {
  627. struct scic_sds_controller *scic = sci_req->owning_controller;
  628. u32 ret_val = 0;
  629. if (readl(&scic->smu_registers->address_modifier) == 0) {
  630. void __iomem *scu_reg_base = scic->scu_registers;
  631. /* get the bytes of data from the Address == BAR1 + 20002Ch + (256*TCi) where
  632. * BAR1 is the scu_registers
  633. * 0x20002C = 0x200000 + 0x2c
  634. * = start of task context SRAM + offset of (type.ssp.data_offset)
  635. * TCi is the io_tag of struct scic_sds_request
  636. */
  637. ret_val = readl(scu_reg_base +
  638. (SCU_TASK_CONTEXT_SRAM + offsetof(struct scu_task_context, type.ssp.data_offset)) +
  639. ((sizeof(struct scu_task_context)) * scic_sds_io_tag_get_index(sci_req->io_tag)));
  640. }
  641. return ret_val;
  642. }
  643. enum sci_status scic_sds_request_start(struct scic_sds_request *sci_req)
  644. {
  645. struct scic_sds_controller *scic = sci_req->owning_controller;
  646. struct scu_task_context *task_context;
  647. enum sci_base_request_states state;
  648. if (sci_req->device_sequence !=
  649. scic_sds_remote_device_get_sequence(sci_req->target_device))
  650. return SCI_FAILURE;
  651. state = sci_req->state_machine.current_state_id;
  652. if (state != SCI_BASE_REQUEST_STATE_CONSTRUCTED) {
  653. dev_warn(scic_to_dev(scic),
  654. "%s: SCIC IO Request requested to start while in wrong "
  655. "state %d\n", __func__, state);
  656. return SCI_FAILURE_INVALID_STATE;
  657. }
  658. /* if necessary, allocate a TCi for the io request object and then will,
  659. * if necessary, copy the constructed TC data into the actual TC buffer.
  660. * If everything is successful the post context field is updated with
  661. * the TCi so the controller can post the request to the hardware.
  662. */
  663. if (sci_req->io_tag == SCI_CONTROLLER_INVALID_IO_TAG)
  664. sci_req->io_tag = scic_controller_allocate_io_tag(scic);
  665. /* Record the IO Tag in the request */
  666. if (sci_req->io_tag != SCI_CONTROLLER_INVALID_IO_TAG) {
  667. task_context = sci_req->task_context_buffer;
  668. task_context->task_index = scic_sds_io_tag_get_index(sci_req->io_tag);
  669. switch (task_context->protocol_type) {
  670. case SCU_TASK_CONTEXT_PROTOCOL_SMP:
  671. case SCU_TASK_CONTEXT_PROTOCOL_SSP:
  672. /* SSP/SMP Frame */
  673. task_context->type.ssp.tag = sci_req->io_tag;
  674. task_context->type.ssp.target_port_transfer_tag =
  675. 0xFFFF;
  676. break;
  677. case SCU_TASK_CONTEXT_PROTOCOL_STP:
  678. /* STP/SATA Frame
  679. * task_context->type.stp.ncq_tag = sci_req->ncq_tag;
  680. */
  681. break;
  682. case SCU_TASK_CONTEXT_PROTOCOL_NONE:
  683. /* / @todo When do we set no protocol type? */
  684. break;
  685. default:
  686. /* This should never happen since we build the IO
  687. * requests */
  688. break;
  689. }
  690. /*
  691. * Check to see if we need to copy the task context buffer
  692. * or have been building into the task context buffer */
  693. if (sci_req->was_tag_assigned_by_user == false)
  694. scic_sds_controller_copy_task_context(scic, sci_req);
  695. /* Add to the post_context the io tag value */
  696. sci_req->post_context |= scic_sds_io_tag_get_index(sci_req->io_tag);
  697. /* Everything is good go ahead and change state */
  698. sci_base_state_machine_change_state(&sci_req->state_machine,
  699. SCI_BASE_REQUEST_STATE_STARTED);
  700. return SCI_SUCCESS;
  701. }
  702. return SCI_FAILURE_INSUFFICIENT_RESOURCES;
  703. }
  704. enum sci_status
  705. scic_sds_io_request_terminate(struct scic_sds_request *sci_req)
  706. {
  707. enum sci_base_request_states state;
  708. state = sci_req->state_machine.current_state_id;
  709. switch (state) {
  710. case SCI_BASE_REQUEST_STATE_CONSTRUCTED:
  711. scic_sds_request_set_status(sci_req,
  712. SCU_TASK_DONE_TASK_ABORT,
  713. SCI_FAILURE_IO_TERMINATED);
  714. sci_base_state_machine_change_state(&sci_req->state_machine,
  715. SCI_BASE_REQUEST_STATE_COMPLETED);
  716. return SCI_SUCCESS;
  717. case SCI_BASE_REQUEST_STATE_STARTED:
  718. case SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION:
  719. case SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE:
  720. case SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION:
  721. case SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE:
  722. case SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE:
  723. case SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE:
  724. case SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE:
  725. case SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE:
  726. case SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE:
  727. case SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE:
  728. case SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE:
  729. case SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE:
  730. case SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE:
  731. case SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE:
  732. sci_base_state_machine_change_state(&sci_req->state_machine,
  733. SCI_BASE_REQUEST_STATE_ABORTING);
  734. return SCI_SUCCESS;
  735. case SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE:
  736. sci_base_state_machine_change_state(&sci_req->state_machine,
  737. SCI_BASE_REQUEST_STATE_ABORTING);
  738. sci_base_state_machine_change_state(&sci_req->state_machine,
  739. SCI_BASE_REQUEST_STATE_COMPLETED);
  740. return SCI_SUCCESS;
  741. case SCI_BASE_REQUEST_STATE_ABORTING:
  742. sci_base_state_machine_change_state(&sci_req->state_machine,
  743. SCI_BASE_REQUEST_STATE_COMPLETED);
  744. return SCI_SUCCESS;
  745. case SCI_BASE_REQUEST_STATE_COMPLETED:
  746. default:
  747. dev_warn(scic_to_dev(sci_req->owning_controller),
  748. "%s: SCIC IO Request requested to abort while in wrong "
  749. "state %d\n",
  750. __func__,
  751. sci_base_state_machine_get_state(&sci_req->state_machine));
  752. break;
  753. }
  754. return SCI_FAILURE_INVALID_STATE;
  755. }
  756. enum sci_status scic_sds_io_request_event_handler(
  757. struct scic_sds_request *request,
  758. u32 event_code)
  759. {
  760. if (request->state_handlers->event_handler)
  761. return request->state_handlers->event_handler(request, event_code);
  762. dev_warn(scic_to_dev(request->owning_controller),
  763. "%s: SCIC IO Request given event code notification %x while "
  764. "in wrong state %d\n",
  765. __func__,
  766. event_code,
  767. sci_base_state_machine_get_state(&request->state_machine));
  768. return SCI_FAILURE_INVALID_STATE;
  769. }
  770. /*
  771. * This function copies response data for requests returning response data
  772. * instead of sense data.
  773. * @sci_req: This parameter specifies the request object for which to copy
  774. * the response data.
  775. */
  776. static void scic_sds_io_request_copy_response(struct scic_sds_request *sci_req)
  777. {
  778. void *resp_buf;
  779. u32 len;
  780. struct ssp_response_iu *ssp_response;
  781. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  782. struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
  783. ssp_response = &sci_req->ssp.rsp;
  784. resp_buf = &isci_tmf->resp.resp_iu;
  785. len = min_t(u32,
  786. SSP_RESP_IU_MAX_SIZE,
  787. be32_to_cpu(ssp_response->response_data_len));
  788. memcpy(resp_buf, ssp_response->resp_data, len);
  789. }
  790. static enum sci_status request_started_state_tc_event(struct scic_sds_request *sci_req,
  791. u32 completion_code)
  792. {
  793. struct ssp_response_iu *resp_iu;
  794. u8 datapres;
  795. /* TODO: Any SDMA return code of other than 0 is bad decode 0x003C0000
  796. * to determine SDMA status
  797. */
  798. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  799. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  800. scic_sds_request_set_status(sci_req,
  801. SCU_TASK_DONE_GOOD,
  802. SCI_SUCCESS);
  803. break;
  804. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EARLY_RESP): {
  805. /* There are times when the SCU hardware will return an early
  806. * response because the io request specified more data than is
  807. * returned by the target device (mode pages, inquiry data,
  808. * etc.). We must check the response stats to see if this is
  809. * truly a failed request or a good request that just got
  810. * completed early.
  811. */
  812. struct ssp_response_iu *resp = &sci_req->ssp.rsp;
  813. ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
  814. sci_swab32_cpy(&sci_req->ssp.rsp,
  815. &sci_req->ssp.rsp,
  816. word_cnt);
  817. if (resp->status == 0) {
  818. scic_sds_request_set_status(sci_req,
  819. SCU_TASK_DONE_GOOD,
  820. SCI_SUCCESS_IO_DONE_EARLY);
  821. } else {
  822. scic_sds_request_set_status(sci_req,
  823. SCU_TASK_DONE_CHECK_RESPONSE,
  824. SCI_FAILURE_IO_RESPONSE_VALID);
  825. }
  826. break;
  827. }
  828. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CHECK_RESPONSE): {
  829. ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
  830. sci_swab32_cpy(&sci_req->ssp.rsp,
  831. &sci_req->ssp.rsp,
  832. word_cnt);
  833. scic_sds_request_set_status(sci_req,
  834. SCU_TASK_DONE_CHECK_RESPONSE,
  835. SCI_FAILURE_IO_RESPONSE_VALID);
  836. break;
  837. }
  838. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RESP_LEN_ERR):
  839. /* TODO With TASK_DONE_RESP_LEN_ERR is the response frame
  840. * guaranteed to be received before this completion status is
  841. * posted?
  842. */
  843. resp_iu = &sci_req->ssp.rsp;
  844. datapres = resp_iu->datapres;
  845. if (datapres == 1 || datapres == 2) {
  846. scic_sds_request_set_status(sci_req,
  847. SCU_TASK_DONE_CHECK_RESPONSE,
  848. SCI_FAILURE_IO_RESPONSE_VALID);
  849. } else
  850. scic_sds_request_set_status(sci_req,
  851. SCU_TASK_DONE_GOOD,
  852. SCI_SUCCESS);
  853. break;
  854. /* only stp device gets suspended. */
  855. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
  856. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_PERR):
  857. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_ERR):
  858. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_DATA_LEN_ERR):
  859. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_ABORT_ERR):
  860. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_WD_LEN):
  861. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
  862. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_RESP):
  863. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_SDBFIS):
  864. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
  865. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDB_ERR):
  866. if (sci_req->protocol == SCIC_STP_PROTOCOL) {
  867. scic_sds_request_set_status(sci_req,
  868. SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  869. SCU_COMPLETION_TL_STATUS_SHIFT,
  870. SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
  871. } else {
  872. scic_sds_request_set_status(sci_req,
  873. SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  874. SCU_COMPLETION_TL_STATUS_SHIFT,
  875. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  876. }
  877. break;
  878. /* both stp/ssp device gets suspended */
  879. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LF_ERR):
  880. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_WRONG_DESTINATION):
  881. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1):
  882. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2):
  883. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3):
  884. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_BAD_DESTINATION):
  885. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_ZONE_VIOLATION):
  886. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY):
  887. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED):
  888. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED):
  889. scic_sds_request_set_status(sci_req,
  890. SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  891. SCU_COMPLETION_TL_STATUS_SHIFT,
  892. SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
  893. break;
  894. /* neither ssp nor stp gets suspended. */
  895. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_CMD_ERR):
  896. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_XR):
  897. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_IU_LEN_ERR):
  898. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDMA_ERR):
  899. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OFFSET_ERR):
  900. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EXCESS_DATA):
  901. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
  902. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
  903. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
  904. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
  905. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_DATA):
  906. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OPEN_FAIL):
  907. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_VIIT_ENTRY_NV):
  908. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_IIT_ENTRY_NV):
  909. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RNCNV_OUTBOUND):
  910. default:
  911. scic_sds_request_set_status(
  912. sci_req,
  913. SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  914. SCU_COMPLETION_TL_STATUS_SHIFT,
  915. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  916. break;
  917. }
  918. /*
  919. * TODO: This is probably wrong for ACK/NAK timeout conditions
  920. */
  921. /* In all cases we will treat this as the completion of the IO req. */
  922. sci_base_state_machine_change_state(&sci_req->state_machine,
  923. SCI_BASE_REQUEST_STATE_COMPLETED);
  924. return SCI_SUCCESS;
  925. }
  926. /*
  927. * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
  928. * object receives a scic_sds_request_complete() request. This method frees up
  929. * any io request resources that have been allocated and transitions the
  930. * request to its final state. Consider stopping the state machine instead of
  931. * transitioning to the final state? enum sci_status SCI_SUCCESS
  932. */
  933. static enum sci_status scic_sds_request_completed_state_complete_handler(
  934. struct scic_sds_request *request)
  935. {
  936. if (request->was_tag_assigned_by_user != true) {
  937. scic_controller_free_io_tag(
  938. request->owning_controller, request->io_tag);
  939. }
  940. if (request->saved_rx_frame_index != SCU_INVALID_FRAME_INDEX) {
  941. scic_sds_controller_release_frame(
  942. request->owning_controller, request->saved_rx_frame_index);
  943. }
  944. sci_base_state_machine_change_state(&request->state_machine,
  945. SCI_BASE_REQUEST_STATE_FINAL);
  946. return SCI_SUCCESS;
  947. }
  948. static enum sci_status request_aborting_state_tc_event(
  949. struct scic_sds_request *sci_req,
  950. u32 completion_code)
  951. {
  952. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  953. case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
  954. case (SCU_TASK_DONE_TASK_ABORT << SCU_COMPLETION_TL_STATUS_SHIFT):
  955. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_TASK_ABORT,
  956. SCI_FAILURE_IO_TERMINATED);
  957. sci_base_state_machine_change_state(&sci_req->state_machine,
  958. SCI_BASE_REQUEST_STATE_COMPLETED);
  959. break;
  960. default:
  961. /* Unless we get some strange error wait for the task abort to complete
  962. * TODO: Should there be a state change for this completion?
  963. */
  964. break;
  965. }
  966. return SCI_SUCCESS;
  967. }
  968. static enum sci_status ssp_task_request_await_tc_event(struct scic_sds_request *sci_req,
  969. u32 completion_code)
  970. {
  971. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  972. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  973. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  974. SCI_SUCCESS);
  975. sci_base_state_machine_change_state(&sci_req->state_machine,
  976. SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE);
  977. break;
  978. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
  979. /* Currently, the decision is to simply allow the task request
  980. * to timeout if the task IU wasn't received successfully.
  981. * There is a potential for receiving multiple task responses if
  982. * we decide to send the task IU again.
  983. */
  984. dev_warn(scic_to_dev(sci_req->owning_controller),
  985. "%s: TaskRequest:0x%p CompletionCode:%x - "
  986. "ACK/NAK timeout\n", __func__, sci_req,
  987. completion_code);
  988. sci_base_state_machine_change_state(&sci_req->state_machine,
  989. SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE);
  990. break;
  991. default:
  992. /* All other completion status cause the IO to be complete. If a NAK
  993. * was received, then it is up to the user to retry the request.
  994. */
  995. scic_sds_request_set_status(sci_req,
  996. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  997. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  998. sci_base_state_machine_change_state(&sci_req->state_machine,
  999. SCI_BASE_REQUEST_STATE_COMPLETED);
  1000. break;
  1001. }
  1002. return SCI_SUCCESS;
  1003. }
  1004. static enum sci_status smp_request_await_response_tc_event(struct scic_sds_request *sci_req,
  1005. u32 completion_code)
  1006. {
  1007. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1008. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1009. /* In the AWAIT RESPONSE state, any TC completion is
  1010. * unexpected. but if the TC has success status, we
  1011. * complete the IO anyway.
  1012. */
  1013. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  1014. SCI_SUCCESS);
  1015. sci_base_state_machine_change_state(&sci_req->state_machine,
  1016. SCI_BASE_REQUEST_STATE_COMPLETED);
  1017. break;
  1018. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
  1019. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
  1020. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
  1021. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
  1022. /* These status has been seen in a specific LSI
  1023. * expander, which sometimes is not able to send smp
  1024. * response within 2 ms. This causes our hardware break
  1025. * the connection and set TC completion with one of
  1026. * these SMP_XXX_XX_ERR status. For these type of error,
  1027. * we ask scic user to retry the request.
  1028. */
  1029. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_SMP_RESP_TO_ERR,
  1030. SCI_FAILURE_RETRY_REQUIRED);
  1031. sci_base_state_machine_change_state(&sci_req->state_machine,
  1032. SCI_BASE_REQUEST_STATE_COMPLETED);
  1033. break;
  1034. default:
  1035. /* All other completion status cause the IO to be complete. If a NAK
  1036. * was received, then it is up to the user to retry the request
  1037. */
  1038. scic_sds_request_set_status(sci_req,
  1039. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1040. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1041. sci_base_state_machine_change_state(&sci_req->state_machine,
  1042. SCI_BASE_REQUEST_STATE_COMPLETED);
  1043. break;
  1044. }
  1045. return SCI_SUCCESS;
  1046. }
  1047. static enum sci_status smp_request_await_tc_event(struct scic_sds_request *sci_req,
  1048. u32 completion_code)
  1049. {
  1050. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1051. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1052. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  1053. SCI_SUCCESS);
  1054. sci_base_state_machine_change_state(&sci_req->state_machine,
  1055. SCI_BASE_REQUEST_STATE_COMPLETED);
  1056. break;
  1057. default:
  1058. /* All other completion status cause the IO to be
  1059. * complete. If a NAK was received, then it is up to
  1060. * the user to retry the request.
  1061. */
  1062. scic_sds_request_set_status(sci_req,
  1063. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1064. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1065. sci_base_state_machine_change_state(&sci_req->state_machine,
  1066. SCI_BASE_REQUEST_STATE_COMPLETED);
  1067. break;
  1068. }
  1069. return SCI_SUCCESS;
  1070. }
  1071. void scic_stp_io_request_set_ncq_tag(struct scic_sds_request *req,
  1072. u16 ncq_tag)
  1073. {
  1074. /**
  1075. * @note This could be made to return an error to the user if the user
  1076. * attempts to set the NCQ tag in the wrong state.
  1077. */
  1078. req->task_context_buffer->type.stp.ncq_tag = ncq_tag;
  1079. }
  1080. /**
  1081. *
  1082. * @sci_req:
  1083. *
  1084. * Get the next SGL element from the request. - Check on which SGL element pair
  1085. * we are working - if working on SLG pair element A - advance to element B -
  1086. * else - check to see if there are more SGL element pairs for this IO request
  1087. * - if there are more SGL element pairs - advance to the next pair and return
  1088. * element A struct scu_sgl_element*
  1089. */
  1090. static struct scu_sgl_element *scic_sds_stp_request_pio_get_next_sgl(struct scic_sds_stp_request *stp_req)
  1091. {
  1092. struct scu_sgl_element *current_sgl;
  1093. struct scic_sds_request *sci_req = to_sci_req(stp_req);
  1094. struct scic_sds_request_pio_sgl *pio_sgl = &stp_req->type.pio.request_current;
  1095. if (pio_sgl->sgl_set == SCU_SGL_ELEMENT_PAIR_A) {
  1096. if (pio_sgl->sgl_pair->B.address_lower == 0 &&
  1097. pio_sgl->sgl_pair->B.address_upper == 0) {
  1098. current_sgl = NULL;
  1099. } else {
  1100. pio_sgl->sgl_set = SCU_SGL_ELEMENT_PAIR_B;
  1101. current_sgl = &pio_sgl->sgl_pair->B;
  1102. }
  1103. } else {
  1104. if (pio_sgl->sgl_pair->next_pair_lower == 0 &&
  1105. pio_sgl->sgl_pair->next_pair_upper == 0) {
  1106. current_sgl = NULL;
  1107. } else {
  1108. u64 phys_addr;
  1109. phys_addr = pio_sgl->sgl_pair->next_pair_upper;
  1110. phys_addr <<= 32;
  1111. phys_addr |= pio_sgl->sgl_pair->next_pair_lower;
  1112. pio_sgl->sgl_pair = scic_request_get_virt_addr(sci_req, phys_addr);
  1113. pio_sgl->sgl_set = SCU_SGL_ELEMENT_PAIR_A;
  1114. current_sgl = &pio_sgl->sgl_pair->A;
  1115. }
  1116. }
  1117. return current_sgl;
  1118. }
  1119. static enum sci_status stp_request_non_data_await_h2d_tc_event(struct scic_sds_request *sci_req,
  1120. u32 completion_code)
  1121. {
  1122. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1123. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1124. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  1125. SCI_SUCCESS);
  1126. sci_base_state_machine_change_state(&sci_req->state_machine,
  1127. SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE);
  1128. break;
  1129. default:
  1130. /* All other completion status cause the IO to be
  1131. * complete. If a NAK was received, then it is up to
  1132. * the user to retry the request.
  1133. */
  1134. scic_sds_request_set_status(sci_req,
  1135. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1136. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1137. sci_base_state_machine_change_state(&sci_req->state_machine,
  1138. SCI_BASE_REQUEST_STATE_COMPLETED);
  1139. break;
  1140. }
  1141. return SCI_SUCCESS;
  1142. }
  1143. #define SCU_MAX_FRAME_BUFFER_SIZE 0x400 /* 1K is the maximum SCU frame data payload */
  1144. /* transmit DATA_FIS from (current sgl + offset) for input
  1145. * parameter length. current sgl and offset is alreay stored in the IO request
  1146. */
  1147. static enum sci_status scic_sds_stp_request_pio_data_out_trasmit_data_frame(
  1148. struct scic_sds_request *sci_req,
  1149. u32 length)
  1150. {
  1151. struct scic_sds_controller *scic = sci_req->owning_controller;
  1152. struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
  1153. struct scu_task_context *task_context;
  1154. struct scu_sgl_element *current_sgl;
  1155. /* Recycle the TC and reconstruct it for sending out DATA FIS containing
  1156. * for the data from current_sgl+offset for the input length
  1157. */
  1158. task_context = scic_sds_controller_get_task_context_buffer(scic,
  1159. sci_req->io_tag);
  1160. if (stp_req->type.pio.request_current.sgl_set == SCU_SGL_ELEMENT_PAIR_A)
  1161. current_sgl = &stp_req->type.pio.request_current.sgl_pair->A;
  1162. else
  1163. current_sgl = &stp_req->type.pio.request_current.sgl_pair->B;
  1164. /* update the TC */
  1165. task_context->command_iu_upper = current_sgl->address_upper;
  1166. task_context->command_iu_lower = current_sgl->address_lower;
  1167. task_context->transfer_length_bytes = length;
  1168. task_context->type.stp.fis_type = FIS_DATA;
  1169. /* send the new TC out. */
  1170. return scic_controller_continue_io(sci_req);
  1171. }
  1172. static enum sci_status scic_sds_stp_request_pio_data_out_transmit_data(struct scic_sds_request *sci_req)
  1173. {
  1174. struct scu_sgl_element *current_sgl;
  1175. u32 sgl_offset;
  1176. u32 remaining_bytes_in_current_sgl = 0;
  1177. enum sci_status status = SCI_SUCCESS;
  1178. struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
  1179. sgl_offset = stp_req->type.pio.request_current.sgl_offset;
  1180. if (stp_req->type.pio.request_current.sgl_set == SCU_SGL_ELEMENT_PAIR_A) {
  1181. current_sgl = &(stp_req->type.pio.request_current.sgl_pair->A);
  1182. remaining_bytes_in_current_sgl = stp_req->type.pio.request_current.sgl_pair->A.length - sgl_offset;
  1183. } else {
  1184. current_sgl = &(stp_req->type.pio.request_current.sgl_pair->B);
  1185. remaining_bytes_in_current_sgl = stp_req->type.pio.request_current.sgl_pair->B.length - sgl_offset;
  1186. }
  1187. if (stp_req->type.pio.pio_transfer_bytes > 0) {
  1188. if (stp_req->type.pio.pio_transfer_bytes >= remaining_bytes_in_current_sgl) {
  1189. /* recycle the TC and send the H2D Data FIS from (current sgl + sgl_offset) and length = remaining_bytes_in_current_sgl */
  1190. status = scic_sds_stp_request_pio_data_out_trasmit_data_frame(sci_req, remaining_bytes_in_current_sgl);
  1191. if (status == SCI_SUCCESS) {
  1192. stp_req->type.pio.pio_transfer_bytes -= remaining_bytes_in_current_sgl;
  1193. /* update the current sgl, sgl_offset and save for future */
  1194. current_sgl = scic_sds_stp_request_pio_get_next_sgl(stp_req);
  1195. sgl_offset = 0;
  1196. }
  1197. } else if (stp_req->type.pio.pio_transfer_bytes < remaining_bytes_in_current_sgl) {
  1198. /* recycle the TC and send the H2D Data FIS from (current sgl + sgl_offset) and length = type.pio.pio_transfer_bytes */
  1199. scic_sds_stp_request_pio_data_out_trasmit_data_frame(sci_req, stp_req->type.pio.pio_transfer_bytes);
  1200. if (status == SCI_SUCCESS) {
  1201. /* Sgl offset will be adjusted and saved for future */
  1202. sgl_offset += stp_req->type.pio.pio_transfer_bytes;
  1203. current_sgl->address_lower += stp_req->type.pio.pio_transfer_bytes;
  1204. stp_req->type.pio.pio_transfer_bytes = 0;
  1205. }
  1206. }
  1207. }
  1208. if (status == SCI_SUCCESS) {
  1209. stp_req->type.pio.request_current.sgl_offset = sgl_offset;
  1210. }
  1211. return status;
  1212. }
  1213. /**
  1214. *
  1215. * @stp_request: The request that is used for the SGL processing.
  1216. * @data_buffer: The buffer of data to be copied.
  1217. * @length: The length of the data transfer.
  1218. *
  1219. * Copy the data from the buffer for the length specified to the IO reqeust SGL
  1220. * specified data region. enum sci_status
  1221. */
  1222. static enum sci_status
  1223. scic_sds_stp_request_pio_data_in_copy_data_buffer(struct scic_sds_stp_request *stp_req,
  1224. u8 *data_buf, u32 len)
  1225. {
  1226. struct scic_sds_request *sci_req;
  1227. struct isci_request *ireq;
  1228. u8 *src_addr;
  1229. int copy_len;
  1230. struct sas_task *task;
  1231. struct scatterlist *sg;
  1232. void *kaddr;
  1233. int total_len = len;
  1234. sci_req = to_sci_req(stp_req);
  1235. ireq = sci_req_to_ireq(sci_req);
  1236. task = isci_request_access_task(ireq);
  1237. src_addr = data_buf;
  1238. if (task->num_scatter > 0) {
  1239. sg = task->scatter;
  1240. while (total_len > 0) {
  1241. struct page *page = sg_page(sg);
  1242. copy_len = min_t(int, total_len, sg_dma_len(sg));
  1243. kaddr = kmap_atomic(page, KM_IRQ0);
  1244. memcpy(kaddr + sg->offset, src_addr, copy_len);
  1245. kunmap_atomic(kaddr, KM_IRQ0);
  1246. total_len -= copy_len;
  1247. src_addr += copy_len;
  1248. sg = sg_next(sg);
  1249. }
  1250. } else {
  1251. BUG_ON(task->total_xfer_len < total_len);
  1252. memcpy(task->scatter, src_addr, total_len);
  1253. }
  1254. return SCI_SUCCESS;
  1255. }
  1256. /**
  1257. *
  1258. * @sci_req: The PIO DATA IN request that is to receive the data.
  1259. * @data_buffer: The buffer to copy from.
  1260. *
  1261. * Copy the data buffer to the io request data region. enum sci_status
  1262. */
  1263. static enum sci_status scic_sds_stp_request_pio_data_in_copy_data(
  1264. struct scic_sds_stp_request *sci_req,
  1265. u8 *data_buffer)
  1266. {
  1267. enum sci_status status;
  1268. /*
  1269. * If there is less than 1K remaining in the transfer request
  1270. * copy just the data for the transfer */
  1271. if (sci_req->type.pio.pio_transfer_bytes < SCU_MAX_FRAME_BUFFER_SIZE) {
  1272. status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
  1273. sci_req, data_buffer, sci_req->type.pio.pio_transfer_bytes);
  1274. if (status == SCI_SUCCESS)
  1275. sci_req->type.pio.pio_transfer_bytes = 0;
  1276. } else {
  1277. /* We are transfering the whole frame so copy */
  1278. status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
  1279. sci_req, data_buffer, SCU_MAX_FRAME_BUFFER_SIZE);
  1280. if (status == SCI_SUCCESS)
  1281. sci_req->type.pio.pio_transfer_bytes -= SCU_MAX_FRAME_BUFFER_SIZE;
  1282. }
  1283. return status;
  1284. }
  1285. static enum sci_status stp_request_pio_await_h2d_completion_tc_event(struct scic_sds_request *sci_req,
  1286. u32 completion_code)
  1287. {
  1288. enum sci_status status = SCI_SUCCESS;
  1289. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1290. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1291. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS);
  1292. sci_base_state_machine_change_state(&sci_req->state_machine,
  1293. SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE);
  1294. break;
  1295. default:
  1296. /* All other completion status cause the IO to be
  1297. * complete. If a NAK was received, then it is up to
  1298. * the user to retry the request.
  1299. */
  1300. scic_sds_request_set_status(sci_req,
  1301. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1302. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1303. sci_base_state_machine_change_state(&sci_req->state_machine,
  1304. SCI_BASE_REQUEST_STATE_COMPLETED);
  1305. break;
  1306. }
  1307. return status;
  1308. }
  1309. static enum sci_status pio_data_out_tx_done_tc_event(struct scic_sds_request *sci_req,
  1310. u32 completion_code)
  1311. {
  1312. enum sci_status status = SCI_SUCCESS;
  1313. bool all_frames_transferred = false;
  1314. struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
  1315. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1316. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1317. /* Transmit data */
  1318. if (stp_req->type.pio.pio_transfer_bytes != 0) {
  1319. status = scic_sds_stp_request_pio_data_out_transmit_data(sci_req);
  1320. if (status == SCI_SUCCESS) {
  1321. if (stp_req->type.pio.pio_transfer_bytes == 0)
  1322. all_frames_transferred = true;
  1323. }
  1324. } else if (stp_req->type.pio.pio_transfer_bytes == 0) {
  1325. /*
  1326. * this will happen if the all data is written at the
  1327. * first time after the pio setup fis is received
  1328. */
  1329. all_frames_transferred = true;
  1330. }
  1331. /* all data transferred. */
  1332. if (all_frames_transferred) {
  1333. /*
  1334. * Change the state to SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_FRAME_SUBSTATE
  1335. * and wait for PIO_SETUP fis / or D2H REg fis. */
  1336. sci_base_state_machine_change_state(
  1337. &sci_req->state_machine,
  1338. SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE
  1339. );
  1340. }
  1341. break;
  1342. default:
  1343. /*
  1344. * All other completion status cause the IO to be complete. If a NAK
  1345. * was received, then it is up to the user to retry the request. */
  1346. scic_sds_request_set_status(
  1347. sci_req,
  1348. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1349. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
  1350. );
  1351. sci_base_state_machine_change_state(
  1352. &sci_req->state_machine,
  1353. SCI_BASE_REQUEST_STATE_COMPLETED
  1354. );
  1355. break;
  1356. }
  1357. return status;
  1358. }
  1359. /**
  1360. *
  1361. * @request: This is the request which is receiving the event.
  1362. * @event_code: This is the event code that the request on which the request is
  1363. * expected to take action.
  1364. *
  1365. * This method will handle any link layer events while waiting for the data
  1366. * frame. enum sci_status SCI_SUCCESS SCI_FAILURE
  1367. */
  1368. static enum sci_status scic_sds_stp_request_pio_data_in_await_data_event_handler(
  1369. struct scic_sds_request *request,
  1370. u32 event_code)
  1371. {
  1372. enum sci_status status;
  1373. switch (scu_get_event_specifier(event_code)) {
  1374. case SCU_TASK_DONE_CRC_ERR << SCU_EVENT_SPECIFIC_CODE_SHIFT:
  1375. /*
  1376. * We are waiting for data and the SCU has R_ERR the data frame.
  1377. * Go back to waiting for the D2H Register FIS */
  1378. sci_base_state_machine_change_state(
  1379. &request->state_machine,
  1380. SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE
  1381. );
  1382. status = SCI_SUCCESS;
  1383. break;
  1384. default:
  1385. dev_err(scic_to_dev(request->owning_controller),
  1386. "%s: SCIC PIO Request 0x%p received unexpected "
  1387. "event 0x%08x\n",
  1388. __func__, request, event_code);
  1389. /* / @todo Should we fail the PIO request when we get an unexpected event? */
  1390. status = SCI_FAILURE;
  1391. break;
  1392. }
  1393. return status;
  1394. }
  1395. static void scic_sds_stp_request_udma_complete_request(
  1396. struct scic_sds_request *request,
  1397. u32 scu_status,
  1398. enum sci_status sci_status)
  1399. {
  1400. scic_sds_request_set_status(request, scu_status, sci_status);
  1401. sci_base_state_machine_change_state(&request->state_machine,
  1402. SCI_BASE_REQUEST_STATE_COMPLETED);
  1403. }
  1404. static enum sci_status scic_sds_stp_request_udma_general_frame_handler(struct scic_sds_request *sci_req,
  1405. u32 frame_index)
  1406. {
  1407. struct scic_sds_controller *scic = sci_req->owning_controller;
  1408. struct dev_to_host_fis *frame_header;
  1409. enum sci_status status;
  1410. u32 *frame_buffer;
  1411. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1412. frame_index,
  1413. (void **)&frame_header);
  1414. if ((status == SCI_SUCCESS) &&
  1415. (frame_header->fis_type == FIS_REGD2H)) {
  1416. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1417. frame_index,
  1418. (void **)&frame_buffer);
  1419. scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
  1420. frame_header,
  1421. frame_buffer);
  1422. }
  1423. scic_sds_controller_release_frame(scic, frame_index);
  1424. return status;
  1425. }
  1426. enum sci_status scic_sds_io_request_frame_handler(struct scic_sds_request *sci_req,
  1427. u32 frame_index)
  1428. {
  1429. struct scic_sds_controller *scic = sci_req->owning_controller;
  1430. struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
  1431. enum sci_base_request_states state;
  1432. enum sci_status status;
  1433. ssize_t word_cnt;
  1434. state = sci_req->state_machine.current_state_id;
  1435. switch (state) {
  1436. case SCI_BASE_REQUEST_STATE_STARTED: {
  1437. struct ssp_frame_hdr ssp_hdr;
  1438. void *frame_header;
  1439. scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1440. frame_index,
  1441. &frame_header);
  1442. word_cnt = sizeof(struct ssp_frame_hdr) / sizeof(u32);
  1443. sci_swab32_cpy(&ssp_hdr, frame_header, word_cnt);
  1444. if (ssp_hdr.frame_type == SSP_RESPONSE) {
  1445. struct ssp_response_iu *resp_iu;
  1446. ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
  1447. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1448. frame_index,
  1449. (void **)&resp_iu);
  1450. sci_swab32_cpy(&sci_req->ssp.rsp, resp_iu, word_cnt);
  1451. resp_iu = &sci_req->ssp.rsp;
  1452. if (resp_iu->datapres == 0x01 ||
  1453. resp_iu->datapres == 0x02) {
  1454. scic_sds_request_set_status(sci_req,
  1455. SCU_TASK_DONE_CHECK_RESPONSE,
  1456. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1457. } else
  1458. scic_sds_request_set_status(sci_req,
  1459. SCU_TASK_DONE_GOOD,
  1460. SCI_SUCCESS);
  1461. } else {
  1462. /* not a response frame, why did it get forwarded? */
  1463. dev_err(scic_to_dev(scic),
  1464. "%s: SCIC IO Request 0x%p received unexpected "
  1465. "frame %d type 0x%02x\n", __func__, sci_req,
  1466. frame_index, ssp_hdr.frame_type);
  1467. }
  1468. /*
  1469. * In any case we are done with this frame buffer return it to the
  1470. * controller
  1471. */
  1472. scic_sds_controller_release_frame(scic, frame_index);
  1473. return SCI_SUCCESS;
  1474. }
  1475. case SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE:
  1476. scic_sds_io_request_copy_response(sci_req);
  1477. sci_base_state_machine_change_state(&sci_req->state_machine,
  1478. SCI_BASE_REQUEST_STATE_COMPLETED);
  1479. scic_sds_controller_release_frame(scic,frame_index);
  1480. return SCI_SUCCESS;
  1481. case SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE: {
  1482. struct smp_resp *rsp_hdr = &sci_req->smp.rsp;
  1483. void *frame_header;
  1484. scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1485. frame_index,
  1486. &frame_header);
  1487. /* byte swap the header. */
  1488. word_cnt = SMP_RESP_HDR_SZ / sizeof(u32);
  1489. sci_swab32_cpy(rsp_hdr, frame_header, word_cnt);
  1490. if (rsp_hdr->frame_type == SMP_RESPONSE) {
  1491. void *smp_resp;
  1492. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1493. frame_index,
  1494. &smp_resp);
  1495. word_cnt = (sizeof(struct smp_req) - SMP_RESP_HDR_SZ) /
  1496. sizeof(u32);
  1497. sci_swab32_cpy(((u8 *) rsp_hdr) + SMP_RESP_HDR_SZ,
  1498. smp_resp, word_cnt);
  1499. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  1500. SCI_SUCCESS);
  1501. sci_base_state_machine_change_state(&sci_req->state_machine,
  1502. SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION);
  1503. } else {
  1504. /* This was not a response frame why did it get forwarded? */
  1505. dev_err(scic_to_dev(scic),
  1506. "%s: SCIC SMP Request 0x%p received unexpected frame "
  1507. "%d type 0x%02x\n", __func__, sci_req,
  1508. frame_index, rsp_hdr->frame_type);
  1509. scic_sds_request_set_status(sci_req,
  1510. SCU_TASK_DONE_SMP_FRM_TYPE_ERR,
  1511. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1512. sci_base_state_machine_change_state(&sci_req->state_machine,
  1513. SCI_BASE_REQUEST_STATE_COMPLETED);
  1514. }
  1515. scic_sds_controller_release_frame(scic, frame_index);
  1516. return SCI_SUCCESS;
  1517. }
  1518. case SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE:
  1519. return scic_sds_stp_request_udma_general_frame_handler(sci_req, frame_index);
  1520. case SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE:
  1521. /* Use the general frame handler to copy the resposne data */
  1522. status = scic_sds_stp_request_udma_general_frame_handler(sci_req, frame_index);
  1523. if (status != SCI_SUCCESS)
  1524. return status;
  1525. scic_sds_stp_request_udma_complete_request(sci_req,
  1526. SCU_TASK_DONE_CHECK_RESPONSE,
  1527. SCI_FAILURE_IO_RESPONSE_VALID);
  1528. return SCI_SUCCESS;
  1529. case SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE: {
  1530. struct dev_to_host_fis *frame_header;
  1531. u32 *frame_buffer;
  1532. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1533. frame_index,
  1534. (void **)&frame_header);
  1535. if (status != SCI_SUCCESS) {
  1536. dev_err(scic_to_dev(scic),
  1537. "%s: SCIC IO Request 0x%p could not get frame header "
  1538. "for frame index %d, status %x\n",
  1539. __func__, stp_req, frame_index, status);
  1540. return status;
  1541. }
  1542. switch (frame_header->fis_type) {
  1543. case FIS_REGD2H:
  1544. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1545. frame_index,
  1546. (void **)&frame_buffer);
  1547. scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
  1548. frame_header,
  1549. frame_buffer);
  1550. /* The command has completed with error */
  1551. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_CHECK_RESPONSE,
  1552. SCI_FAILURE_IO_RESPONSE_VALID);
  1553. break;
  1554. default:
  1555. dev_warn(scic_to_dev(scic),
  1556. "%s: IO Request:0x%p Frame Id:%d protocol "
  1557. "violation occurred\n", __func__, stp_req,
  1558. frame_index);
  1559. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_UNEXP_FIS,
  1560. SCI_FAILURE_PROTOCOL_VIOLATION);
  1561. break;
  1562. }
  1563. sci_base_state_machine_change_state(&sci_req->state_machine,
  1564. SCI_BASE_REQUEST_STATE_COMPLETED);
  1565. /* Frame has been decoded return it to the controller */
  1566. scic_sds_controller_release_frame(scic, frame_index);
  1567. return status;
  1568. }
  1569. case SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE: {
  1570. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  1571. struct sas_task *task = isci_request_access_task(ireq);
  1572. struct dev_to_host_fis *frame_header;
  1573. u32 *frame_buffer;
  1574. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1575. frame_index,
  1576. (void **)&frame_header);
  1577. if (status != SCI_SUCCESS) {
  1578. dev_err(scic_to_dev(scic),
  1579. "%s: SCIC IO Request 0x%p could not get frame header "
  1580. "for frame index %d, status %x\n",
  1581. __func__, stp_req, frame_index, status);
  1582. return status;
  1583. }
  1584. switch (frame_header->fis_type) {
  1585. case FIS_PIO_SETUP:
  1586. /* Get from the frame buffer the PIO Setup Data */
  1587. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1588. frame_index,
  1589. (void **)&frame_buffer);
  1590. /* Get the data from the PIO Setup The SCU Hardware returns
  1591. * first word in the frame_header and the rest of the data is in
  1592. * the frame buffer so we need to back up one dword
  1593. */
  1594. /* transfer_count: first 16bits in the 4th dword */
  1595. stp_req->type.pio.pio_transfer_bytes = frame_buffer[3] & 0xffff;
  1596. /* ending_status: 4th byte in the 3rd dword */
  1597. stp_req->type.pio.ending_status = (frame_buffer[2] >> 24) & 0xff;
  1598. scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
  1599. frame_header,
  1600. frame_buffer);
  1601. sci_req->stp.rsp.status = stp_req->type.pio.ending_status;
  1602. /* The next state is dependent on whether the
  1603. * request was PIO Data-in or Data out
  1604. */
  1605. if (task->data_dir == DMA_FROM_DEVICE) {
  1606. sci_base_state_machine_change_state(&sci_req->state_machine,
  1607. SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE);
  1608. } else if (task->data_dir == DMA_TO_DEVICE) {
  1609. /* Transmit data */
  1610. status = scic_sds_stp_request_pio_data_out_transmit_data(sci_req);
  1611. if (status != SCI_SUCCESS)
  1612. break;
  1613. sci_base_state_machine_change_state(&sci_req->state_machine,
  1614. SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE);
  1615. }
  1616. break;
  1617. case FIS_SETDEVBITS:
  1618. sci_base_state_machine_change_state(&sci_req->state_machine,
  1619. SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE);
  1620. break;
  1621. case FIS_REGD2H:
  1622. if (frame_header->status & ATA_BUSY) {
  1623. /* Now why is the drive sending a D2H Register FIS when
  1624. * it is still busy? Do nothing since we are still in
  1625. * the right state.
  1626. */
  1627. dev_dbg(scic_to_dev(scic),
  1628. "%s: SCIC PIO Request 0x%p received "
  1629. "D2H Register FIS with BSY status "
  1630. "0x%x\n", __func__, stp_req,
  1631. frame_header->status);
  1632. break;
  1633. }
  1634. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1635. frame_index,
  1636. (void **)&frame_buffer);
  1637. scic_sds_controller_copy_sata_response(&sci_req->stp.req,
  1638. frame_header,
  1639. frame_buffer);
  1640. scic_sds_request_set_status(sci_req,
  1641. SCU_TASK_DONE_CHECK_RESPONSE,
  1642. SCI_FAILURE_IO_RESPONSE_VALID);
  1643. sci_base_state_machine_change_state(&sci_req->state_machine,
  1644. SCI_BASE_REQUEST_STATE_COMPLETED);
  1645. break;
  1646. default:
  1647. /* FIXME: what do we do here? */
  1648. break;
  1649. }
  1650. /* Frame is decoded return it to the controller */
  1651. scic_sds_controller_release_frame(scic, frame_index);
  1652. return status;
  1653. }
  1654. case SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE: {
  1655. struct dev_to_host_fis *frame_header;
  1656. struct sata_fis_data *frame_buffer;
  1657. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1658. frame_index,
  1659. (void **)&frame_header);
  1660. if (status != SCI_SUCCESS) {
  1661. dev_err(scic_to_dev(scic),
  1662. "%s: SCIC IO Request 0x%p could not get frame header "
  1663. "for frame index %d, status %x\n",
  1664. __func__, stp_req, frame_index, status);
  1665. return status;
  1666. }
  1667. if (frame_header->fis_type != FIS_DATA) {
  1668. dev_err(scic_to_dev(scic),
  1669. "%s: SCIC PIO Request 0x%p received frame %d "
  1670. "with fis type 0x%02x when expecting a data "
  1671. "fis.\n", __func__, stp_req, frame_index,
  1672. frame_header->fis_type);
  1673. scic_sds_request_set_status(sci_req,
  1674. SCU_TASK_DONE_GOOD,
  1675. SCI_FAILURE_IO_REQUIRES_SCSI_ABORT);
  1676. sci_base_state_machine_change_state(&sci_req->state_machine,
  1677. SCI_BASE_REQUEST_STATE_COMPLETED);
  1678. /* Frame is decoded return it to the controller */
  1679. scic_sds_controller_release_frame(scic, frame_index);
  1680. return status;
  1681. }
  1682. if (stp_req->type.pio.request_current.sgl_pair == NULL) {
  1683. sci_req->saved_rx_frame_index = frame_index;
  1684. stp_req->type.pio.pio_transfer_bytes = 0;
  1685. } else {
  1686. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1687. frame_index,
  1688. (void **)&frame_buffer);
  1689. status = scic_sds_stp_request_pio_data_in_copy_data(stp_req,
  1690. (u8 *)frame_buffer);
  1691. /* Frame is decoded return it to the controller */
  1692. scic_sds_controller_release_frame(scic, frame_index);
  1693. }
  1694. /* Check for the end of the transfer, are there more
  1695. * bytes remaining for this data transfer
  1696. */
  1697. if (status != SCI_SUCCESS ||
  1698. stp_req->type.pio.pio_transfer_bytes != 0)
  1699. return status;
  1700. if ((stp_req->type.pio.ending_status & ATA_BUSY) == 0) {
  1701. scic_sds_request_set_status(sci_req,
  1702. SCU_TASK_DONE_CHECK_RESPONSE,
  1703. SCI_FAILURE_IO_RESPONSE_VALID);
  1704. sci_base_state_machine_change_state(&sci_req->state_machine,
  1705. SCI_BASE_REQUEST_STATE_COMPLETED);
  1706. } else {
  1707. sci_base_state_machine_change_state(&sci_req->state_machine,
  1708. SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE);
  1709. }
  1710. return status;
  1711. }
  1712. case SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE: {
  1713. struct dev_to_host_fis *frame_header;
  1714. u32 *frame_buffer;
  1715. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1716. frame_index,
  1717. (void **)&frame_header);
  1718. if (status != SCI_SUCCESS) {
  1719. dev_err(scic_to_dev(scic),
  1720. "%s: SCIC IO Request 0x%p could not get frame header "
  1721. "for frame index %d, status %x\n",
  1722. __func__, stp_req, frame_index, status);
  1723. return status;
  1724. }
  1725. switch (frame_header->fis_type) {
  1726. case FIS_REGD2H:
  1727. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1728. frame_index,
  1729. (void **)&frame_buffer);
  1730. scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
  1731. frame_header,
  1732. frame_buffer);
  1733. /* The command has completed with error */
  1734. scic_sds_request_set_status(sci_req,
  1735. SCU_TASK_DONE_CHECK_RESPONSE,
  1736. SCI_FAILURE_IO_RESPONSE_VALID);
  1737. break;
  1738. default:
  1739. dev_warn(scic_to_dev(scic),
  1740. "%s: IO Request:0x%p Frame Id:%d protocol "
  1741. "violation occurred\n", __func__, stp_req,
  1742. frame_index);
  1743. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_UNEXP_FIS,
  1744. SCI_FAILURE_PROTOCOL_VIOLATION);
  1745. break;
  1746. }
  1747. sci_base_state_machine_change_state(&sci_req->state_machine,
  1748. SCI_BASE_REQUEST_STATE_COMPLETED);
  1749. /* Frame has been decoded return it to the controller */
  1750. scic_sds_controller_release_frame(scic, frame_index);
  1751. return status;
  1752. }
  1753. case SCI_BASE_REQUEST_STATE_ABORTING:
  1754. /* TODO: Is it even possible to get an unsolicited frame in the
  1755. * aborting state?
  1756. */
  1757. scic_sds_controller_release_frame(scic, frame_index);
  1758. return SCI_SUCCESS;
  1759. default:
  1760. dev_warn(scic_to_dev(scic),
  1761. "%s: SCIC IO Request given unexpected frame %x while in "
  1762. "state %d\n", __func__, frame_index, state);
  1763. scic_sds_controller_release_frame(scic, frame_index);
  1764. return SCI_FAILURE_INVALID_STATE;
  1765. }
  1766. }
  1767. static enum sci_status stp_request_udma_await_tc_event(struct scic_sds_request *sci_req,
  1768. u32 completion_code)
  1769. {
  1770. enum sci_status status = SCI_SUCCESS;
  1771. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1772. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1773. scic_sds_stp_request_udma_complete_request(sci_req,
  1774. SCU_TASK_DONE_GOOD,
  1775. SCI_SUCCESS);
  1776. break;
  1777. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_FIS):
  1778. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
  1779. /* We must check ther response buffer to see if the D2H
  1780. * Register FIS was received before we got the TC
  1781. * completion.
  1782. */
  1783. if (sci_req->stp.rsp.fis_type == FIS_REGD2H) {
  1784. scic_sds_remote_device_suspend(sci_req->target_device,
  1785. SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
  1786. scic_sds_stp_request_udma_complete_request(sci_req,
  1787. SCU_TASK_DONE_CHECK_RESPONSE,
  1788. SCI_FAILURE_IO_RESPONSE_VALID);
  1789. } else {
  1790. /* If we have an error completion status for the
  1791. * TC then we can expect a D2H register FIS from
  1792. * the device so we must change state to wait
  1793. * for it
  1794. */
  1795. sci_base_state_machine_change_state(&sci_req->state_machine,
  1796. SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE);
  1797. }
  1798. break;
  1799. /* TODO Check to see if any of these completion status need to
  1800. * wait for the device to host register fis.
  1801. */
  1802. /* TODO We can retry the command for SCU_TASK_DONE_CMD_LL_R_ERR
  1803. * - this comes only for B0
  1804. */
  1805. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_INV_FIS_LEN):
  1806. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
  1807. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_R_ERR):
  1808. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CMD_LL_R_ERR):
  1809. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CRC_ERR):
  1810. scic_sds_remote_device_suspend(sci_req->target_device,
  1811. SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
  1812. /* Fall through to the default case */
  1813. default:
  1814. /* All other completion status cause the IO to be complete. */
  1815. scic_sds_stp_request_udma_complete_request(sci_req,
  1816. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1817. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1818. break;
  1819. }
  1820. return status;
  1821. }
  1822. static enum sci_status stp_request_soft_reset_await_h2d_asserted_tc_event(struct scic_sds_request *sci_req,
  1823. u32 completion_code)
  1824. {
  1825. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1826. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1827. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  1828. SCI_SUCCESS);
  1829. sci_base_state_machine_change_state(&sci_req->state_machine,
  1830. SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE);
  1831. break;
  1832. default:
  1833. /*
  1834. * All other completion status cause the IO to be complete. If a NAK
  1835. * was received, then it is up to the user to retry the request. */
  1836. scic_sds_request_set_status(sci_req,
  1837. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1838. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1839. sci_base_state_machine_change_state(&sci_req->state_machine,
  1840. SCI_BASE_REQUEST_STATE_COMPLETED);
  1841. break;
  1842. }
  1843. return SCI_SUCCESS;
  1844. }
  1845. static enum sci_status stp_request_soft_reset_await_h2d_diagnostic_tc_event(
  1846. struct scic_sds_request *sci_req,
  1847. u32 completion_code)
  1848. {
  1849. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1850. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1851. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  1852. SCI_SUCCESS);
  1853. sci_base_state_machine_change_state(&sci_req->state_machine,
  1854. SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE);
  1855. break;
  1856. default:
  1857. /* All other completion status cause the IO to be complete. If
  1858. * a NAK was received, then it is up to the user to retry the
  1859. * request.
  1860. */
  1861. scic_sds_request_set_status(sci_req,
  1862. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1863. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1864. sci_base_state_machine_change_state(&sci_req->state_machine,
  1865. SCI_BASE_REQUEST_STATE_COMPLETED);
  1866. break;
  1867. }
  1868. return SCI_SUCCESS;
  1869. }
  1870. enum sci_status
  1871. scic_sds_io_request_tc_completion(struct scic_sds_request *sci_req, u32 completion_code)
  1872. {
  1873. enum sci_base_request_states state;
  1874. struct scic_sds_controller *scic = sci_req->owning_controller;
  1875. state = sci_req->state_machine.current_state_id;
  1876. switch (state) {
  1877. case SCI_BASE_REQUEST_STATE_STARTED:
  1878. return request_started_state_tc_event(sci_req, completion_code);
  1879. case SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION:
  1880. return ssp_task_request_await_tc_event(sci_req, completion_code);
  1881. case SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE:
  1882. return smp_request_await_response_tc_event(sci_req, completion_code);
  1883. case SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION:
  1884. return smp_request_await_tc_event(sci_req, completion_code);
  1885. case SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE:
  1886. return stp_request_udma_await_tc_event(sci_req, completion_code);
  1887. case SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE:
  1888. return stp_request_non_data_await_h2d_tc_event(sci_req, completion_code);
  1889. case SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE:
  1890. return stp_request_pio_await_h2d_completion_tc_event(sci_req, completion_code);
  1891. case SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE:
  1892. return pio_data_out_tx_done_tc_event(sci_req, completion_code);
  1893. case SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE:
  1894. return stp_request_soft_reset_await_h2d_asserted_tc_event(sci_req, completion_code);
  1895. case SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE:
  1896. return stp_request_soft_reset_await_h2d_diagnostic_tc_event(sci_req, completion_code);
  1897. case SCI_BASE_REQUEST_STATE_ABORTING:
  1898. return request_aborting_state_tc_event(sci_req, completion_code);
  1899. default:
  1900. dev_warn(scic_to_dev(scic),
  1901. "%s: SCIC IO Request given task completion notification %x "
  1902. "while in wrong state %d\n", __func__, completion_code,
  1903. state);
  1904. return SCI_FAILURE_INVALID_STATE;
  1905. }
  1906. }
  1907. static const struct scic_sds_io_request_state_handler scic_sds_request_state_handler_table[] = {
  1908. [SCI_BASE_REQUEST_STATE_INITIAL] = {},
  1909. [SCI_BASE_REQUEST_STATE_CONSTRUCTED] = {},
  1910. [SCI_BASE_REQUEST_STATE_STARTED] = { },
  1911. [SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION] = { },
  1912. [SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE] = { },
  1913. [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE] = { },
  1914. [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION] = { },
  1915. [SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE] = { },
  1916. [SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE] = { },
  1917. [SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE] = { },
  1918. [SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE] = { },
  1919. [SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE] = { },
  1920. [SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE] = { },
  1921. [SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE] = {
  1922. .event_handler = scic_sds_stp_request_pio_data_in_await_data_event_handler,
  1923. },
  1924. [SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE] = { },
  1925. [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE] = { },
  1926. [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE] = { },
  1927. [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE] = { },
  1928. [SCI_BASE_REQUEST_STATE_COMPLETED] = {
  1929. .complete_handler = scic_sds_request_completed_state_complete_handler,
  1930. },
  1931. [SCI_BASE_REQUEST_STATE_ABORTING] = { },
  1932. [SCI_BASE_REQUEST_STATE_FINAL] = { },
  1933. };
  1934. /**
  1935. * isci_request_process_response_iu() - This function sets the status and
  1936. * response iu, in the task struct, from the request object for the upper
  1937. * layer driver.
  1938. * @sas_task: This parameter is the task struct from the upper layer driver.
  1939. * @resp_iu: This parameter points to the response iu of the completed request.
  1940. * @dev: This parameter specifies the linux device struct.
  1941. *
  1942. * none.
  1943. */
  1944. static void isci_request_process_response_iu(
  1945. struct sas_task *task,
  1946. struct ssp_response_iu *resp_iu,
  1947. struct device *dev)
  1948. {
  1949. dev_dbg(dev,
  1950. "%s: resp_iu = %p "
  1951. "resp_iu->status = 0x%x,\nresp_iu->datapres = %d "
  1952. "resp_iu->response_data_len = %x, "
  1953. "resp_iu->sense_data_len = %x\nrepsonse data: ",
  1954. __func__,
  1955. resp_iu,
  1956. resp_iu->status,
  1957. resp_iu->datapres,
  1958. resp_iu->response_data_len,
  1959. resp_iu->sense_data_len);
  1960. task->task_status.stat = resp_iu->status;
  1961. /* libsas updates the task status fields based on the response iu. */
  1962. sas_ssp_task_response(dev, task, resp_iu);
  1963. }
  1964. /**
  1965. * isci_request_set_open_reject_status() - This function prepares the I/O
  1966. * completion for OPEN_REJECT conditions.
  1967. * @request: This parameter is the completed isci_request object.
  1968. * @response_ptr: This parameter specifies the service response for the I/O.
  1969. * @status_ptr: This parameter specifies the exec status for the I/O.
  1970. * @complete_to_host_ptr: This parameter specifies the action to be taken by
  1971. * the LLDD with respect to completing this request or forcing an abort
  1972. * condition on the I/O.
  1973. * @open_rej_reason: This parameter specifies the encoded reason for the
  1974. * abandon-class reject.
  1975. *
  1976. * none.
  1977. */
  1978. static void isci_request_set_open_reject_status(
  1979. struct isci_request *request,
  1980. struct sas_task *task,
  1981. enum service_response *response_ptr,
  1982. enum exec_status *status_ptr,
  1983. enum isci_completion_selection *complete_to_host_ptr,
  1984. enum sas_open_rej_reason open_rej_reason)
  1985. {
  1986. /* Task in the target is done. */
  1987. request->complete_in_target = true;
  1988. *response_ptr = SAS_TASK_UNDELIVERED;
  1989. *status_ptr = SAS_OPEN_REJECT;
  1990. *complete_to_host_ptr = isci_perform_normal_io_completion;
  1991. task->task_status.open_rej_reason = open_rej_reason;
  1992. }
  1993. /**
  1994. * isci_request_handle_controller_specific_errors() - This function decodes
  1995. * controller-specific I/O completion error conditions.
  1996. * @request: This parameter is the completed isci_request object.
  1997. * @response_ptr: This parameter specifies the service response for the I/O.
  1998. * @status_ptr: This parameter specifies the exec status for the I/O.
  1999. * @complete_to_host_ptr: This parameter specifies the action to be taken by
  2000. * the LLDD with respect to completing this request or forcing an abort
  2001. * condition on the I/O.
  2002. *
  2003. * none.
  2004. */
  2005. static void isci_request_handle_controller_specific_errors(
  2006. struct isci_remote_device *isci_device,
  2007. struct isci_request *request,
  2008. struct sas_task *task,
  2009. enum service_response *response_ptr,
  2010. enum exec_status *status_ptr,
  2011. enum isci_completion_selection *complete_to_host_ptr)
  2012. {
  2013. unsigned int cstatus;
  2014. cstatus = request->sci.scu_status;
  2015. dev_dbg(&request->isci_host->pdev->dev,
  2016. "%s: %p SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR "
  2017. "- controller status = 0x%x\n",
  2018. __func__, request, cstatus);
  2019. /* Decode the controller-specific errors; most
  2020. * important is to recognize those conditions in which
  2021. * the target may still have a task outstanding that
  2022. * must be aborted.
  2023. *
  2024. * Note that there are SCU completion codes being
  2025. * named in the decode below for which SCIC has already
  2026. * done work to handle them in a way other than as
  2027. * a controller-specific completion code; these are left
  2028. * in the decode below for completeness sake.
  2029. */
  2030. switch (cstatus) {
  2031. case SCU_TASK_DONE_DMASETUP_DIRERR:
  2032. /* Also SCU_TASK_DONE_SMP_FRM_TYPE_ERR: */
  2033. case SCU_TASK_DONE_XFERCNT_ERR:
  2034. /* Also SCU_TASK_DONE_SMP_UFI_ERR: */
  2035. if (task->task_proto == SAS_PROTOCOL_SMP) {
  2036. /* SCU_TASK_DONE_SMP_UFI_ERR == Task Done. */
  2037. *response_ptr = SAS_TASK_COMPLETE;
  2038. /* See if the device has been/is being stopped. Note
  2039. * that we ignore the quiesce state, since we are
  2040. * concerned about the actual device state.
  2041. */
  2042. if ((isci_device->status == isci_stopping) ||
  2043. (isci_device->status == isci_stopped))
  2044. *status_ptr = SAS_DEVICE_UNKNOWN;
  2045. else
  2046. *status_ptr = SAS_ABORTED_TASK;
  2047. request->complete_in_target = true;
  2048. *complete_to_host_ptr =
  2049. isci_perform_normal_io_completion;
  2050. } else {
  2051. /* Task in the target is not done. */
  2052. *response_ptr = SAS_TASK_UNDELIVERED;
  2053. if ((isci_device->status == isci_stopping) ||
  2054. (isci_device->status == isci_stopped))
  2055. *status_ptr = SAS_DEVICE_UNKNOWN;
  2056. else
  2057. *status_ptr = SAM_STAT_TASK_ABORTED;
  2058. request->complete_in_target = false;
  2059. *complete_to_host_ptr =
  2060. isci_perform_error_io_completion;
  2061. }
  2062. break;
  2063. case SCU_TASK_DONE_CRC_ERR:
  2064. case SCU_TASK_DONE_NAK_CMD_ERR:
  2065. case SCU_TASK_DONE_EXCESS_DATA:
  2066. case SCU_TASK_DONE_UNEXP_FIS:
  2067. /* Also SCU_TASK_DONE_UNEXP_RESP: */
  2068. case SCU_TASK_DONE_VIIT_ENTRY_NV: /* TODO - conditions? */
  2069. case SCU_TASK_DONE_IIT_ENTRY_NV: /* TODO - conditions? */
  2070. case SCU_TASK_DONE_RNCNV_OUTBOUND: /* TODO - conditions? */
  2071. /* These are conditions in which the target
  2072. * has completed the task, so that no cleanup
  2073. * is necessary.
  2074. */
  2075. *response_ptr = SAS_TASK_COMPLETE;
  2076. /* See if the device has been/is being stopped. Note
  2077. * that we ignore the quiesce state, since we are
  2078. * concerned about the actual device state.
  2079. */
  2080. if ((isci_device->status == isci_stopping) ||
  2081. (isci_device->status == isci_stopped))
  2082. *status_ptr = SAS_DEVICE_UNKNOWN;
  2083. else
  2084. *status_ptr = SAS_ABORTED_TASK;
  2085. request->complete_in_target = true;
  2086. *complete_to_host_ptr = isci_perform_normal_io_completion;
  2087. break;
  2088. /* Note that the only open reject completion codes seen here will be
  2089. * abandon-class codes; all others are automatically retried in the SCU.
  2090. */
  2091. case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
  2092. isci_request_set_open_reject_status(
  2093. request, task, response_ptr, status_ptr,
  2094. complete_to_host_ptr, SAS_OREJ_WRONG_DEST);
  2095. break;
  2096. case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
  2097. /* Note - the return of AB0 will change when
  2098. * libsas implements detection of zone violations.
  2099. */
  2100. isci_request_set_open_reject_status(
  2101. request, task, response_ptr, status_ptr,
  2102. complete_to_host_ptr, SAS_OREJ_RESV_AB0);
  2103. break;
  2104. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
  2105. isci_request_set_open_reject_status(
  2106. request, task, response_ptr, status_ptr,
  2107. complete_to_host_ptr, SAS_OREJ_RESV_AB1);
  2108. break;
  2109. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
  2110. isci_request_set_open_reject_status(
  2111. request, task, response_ptr, status_ptr,
  2112. complete_to_host_ptr, SAS_OREJ_RESV_AB2);
  2113. break;
  2114. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
  2115. isci_request_set_open_reject_status(
  2116. request, task, response_ptr, status_ptr,
  2117. complete_to_host_ptr, SAS_OREJ_RESV_AB3);
  2118. break;
  2119. case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
  2120. isci_request_set_open_reject_status(
  2121. request, task, response_ptr, status_ptr,
  2122. complete_to_host_ptr, SAS_OREJ_BAD_DEST);
  2123. break;
  2124. case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
  2125. isci_request_set_open_reject_status(
  2126. request, task, response_ptr, status_ptr,
  2127. complete_to_host_ptr, SAS_OREJ_STP_NORES);
  2128. break;
  2129. case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
  2130. isci_request_set_open_reject_status(
  2131. request, task, response_ptr, status_ptr,
  2132. complete_to_host_ptr, SAS_OREJ_EPROTO);
  2133. break;
  2134. case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
  2135. isci_request_set_open_reject_status(
  2136. request, task, response_ptr, status_ptr,
  2137. complete_to_host_ptr, SAS_OREJ_CONN_RATE);
  2138. break;
  2139. case SCU_TASK_DONE_LL_R_ERR:
  2140. /* Also SCU_TASK_DONE_ACK_NAK_TO: */
  2141. case SCU_TASK_DONE_LL_PERR:
  2142. case SCU_TASK_DONE_LL_SY_TERM:
  2143. /* Also SCU_TASK_DONE_NAK_ERR:*/
  2144. case SCU_TASK_DONE_LL_LF_TERM:
  2145. /* Also SCU_TASK_DONE_DATA_LEN_ERR: */
  2146. case SCU_TASK_DONE_LL_ABORT_ERR:
  2147. case SCU_TASK_DONE_SEQ_INV_TYPE:
  2148. /* Also SCU_TASK_DONE_UNEXP_XR: */
  2149. case SCU_TASK_DONE_XR_IU_LEN_ERR:
  2150. case SCU_TASK_DONE_INV_FIS_LEN:
  2151. /* Also SCU_TASK_DONE_XR_WD_LEN: */
  2152. case SCU_TASK_DONE_SDMA_ERR:
  2153. case SCU_TASK_DONE_OFFSET_ERR:
  2154. case SCU_TASK_DONE_MAX_PLD_ERR:
  2155. case SCU_TASK_DONE_LF_ERR:
  2156. case SCU_TASK_DONE_SMP_RESP_TO_ERR: /* Escalate to dev reset? */
  2157. case SCU_TASK_DONE_SMP_LL_RX_ERR:
  2158. case SCU_TASK_DONE_UNEXP_DATA:
  2159. case SCU_TASK_DONE_UNEXP_SDBFIS:
  2160. case SCU_TASK_DONE_REG_ERR:
  2161. case SCU_TASK_DONE_SDB_ERR:
  2162. case SCU_TASK_DONE_TASK_ABORT:
  2163. default:
  2164. /* Task in the target is not done. */
  2165. *response_ptr = SAS_TASK_UNDELIVERED;
  2166. *status_ptr = SAM_STAT_TASK_ABORTED;
  2167. request->complete_in_target = false;
  2168. *complete_to_host_ptr = isci_perform_error_io_completion;
  2169. break;
  2170. }
  2171. }
  2172. /**
  2173. * isci_task_save_for_upper_layer_completion() - This function saves the
  2174. * request for later completion to the upper layer driver.
  2175. * @host: This parameter is a pointer to the host on which the the request
  2176. * should be queued (either as an error or success).
  2177. * @request: This parameter is the completed request.
  2178. * @response: This parameter is the response code for the completed task.
  2179. * @status: This parameter is the status code for the completed task.
  2180. *
  2181. * none.
  2182. */
  2183. static void isci_task_save_for_upper_layer_completion(
  2184. struct isci_host *host,
  2185. struct isci_request *request,
  2186. enum service_response response,
  2187. enum exec_status status,
  2188. enum isci_completion_selection task_notification_selection)
  2189. {
  2190. struct sas_task *task = isci_request_access_task(request);
  2191. task_notification_selection
  2192. = isci_task_set_completion_status(task, response, status,
  2193. task_notification_selection);
  2194. /* Tasks aborted specifically by a call to the lldd_abort_task
  2195. * function should not be completed to the host in the regular path.
  2196. */
  2197. switch (task_notification_selection) {
  2198. case isci_perform_normal_io_completion:
  2199. /* Normal notification (task_done) */
  2200. dev_dbg(&host->pdev->dev,
  2201. "%s: Normal - task = %p, response=%d (%d), status=%d (%d)\n",
  2202. __func__,
  2203. task,
  2204. task->task_status.resp, response,
  2205. task->task_status.stat, status);
  2206. /* Add to the completed list. */
  2207. list_add(&request->completed_node,
  2208. &host->requests_to_complete);
  2209. /* Take the request off the device's pending request list. */
  2210. list_del_init(&request->dev_node);
  2211. break;
  2212. case isci_perform_aborted_io_completion:
  2213. /* No notification to libsas because this request is
  2214. * already in the abort path.
  2215. */
  2216. dev_warn(&host->pdev->dev,
  2217. "%s: Aborted - task = %p, response=%d (%d), status=%d (%d)\n",
  2218. __func__,
  2219. task,
  2220. task->task_status.resp, response,
  2221. task->task_status.stat, status);
  2222. /* Wake up whatever process was waiting for this
  2223. * request to complete.
  2224. */
  2225. WARN_ON(request->io_request_completion == NULL);
  2226. if (request->io_request_completion != NULL) {
  2227. /* Signal whoever is waiting that this
  2228. * request is complete.
  2229. */
  2230. complete(request->io_request_completion);
  2231. }
  2232. break;
  2233. case isci_perform_error_io_completion:
  2234. /* Use sas_task_abort */
  2235. dev_warn(&host->pdev->dev,
  2236. "%s: Error - task = %p, response=%d (%d), status=%d (%d)\n",
  2237. __func__,
  2238. task,
  2239. task->task_status.resp, response,
  2240. task->task_status.stat, status);
  2241. /* Add to the aborted list. */
  2242. list_add(&request->completed_node,
  2243. &host->requests_to_errorback);
  2244. break;
  2245. default:
  2246. dev_warn(&host->pdev->dev,
  2247. "%s: Unknown - task = %p, response=%d (%d), status=%d (%d)\n",
  2248. __func__,
  2249. task,
  2250. task->task_status.resp, response,
  2251. task->task_status.stat, status);
  2252. /* Add to the error to libsas list. */
  2253. list_add(&request->completed_node,
  2254. &host->requests_to_errorback);
  2255. break;
  2256. }
  2257. }
  2258. static void isci_request_io_request_complete(struct isci_host *isci_host,
  2259. struct isci_request *request,
  2260. enum sci_io_status completion_status)
  2261. {
  2262. struct sas_task *task = isci_request_access_task(request);
  2263. struct ssp_response_iu *resp_iu;
  2264. void *resp_buf;
  2265. unsigned long task_flags;
  2266. struct isci_remote_device *isci_device = request->isci_device;
  2267. enum service_response response = SAS_TASK_UNDELIVERED;
  2268. enum exec_status status = SAS_ABORTED_TASK;
  2269. enum isci_request_status request_status;
  2270. enum isci_completion_selection complete_to_host
  2271. = isci_perform_normal_io_completion;
  2272. dev_dbg(&isci_host->pdev->dev,
  2273. "%s: request = %p, task = %p,\n"
  2274. "task->data_dir = %d completion_status = 0x%x\n",
  2275. __func__,
  2276. request,
  2277. task,
  2278. task->data_dir,
  2279. completion_status);
  2280. spin_lock(&request->state_lock);
  2281. request_status = isci_request_get_state(request);
  2282. /* Decode the request status. Note that if the request has been
  2283. * aborted by a task management function, we don't care
  2284. * what the status is.
  2285. */
  2286. switch (request_status) {
  2287. case aborted:
  2288. /* "aborted" indicates that the request was aborted by a task
  2289. * management function, since once a task management request is
  2290. * perfomed by the device, the request only completes because
  2291. * of the subsequent driver terminate.
  2292. *
  2293. * Aborted also means an external thread is explicitly managing
  2294. * this request, so that we do not complete it up the stack.
  2295. *
  2296. * The target is still there (since the TMF was successful).
  2297. */
  2298. request->complete_in_target = true;
  2299. response = SAS_TASK_COMPLETE;
  2300. /* See if the device has been/is being stopped. Note
  2301. * that we ignore the quiesce state, since we are
  2302. * concerned about the actual device state.
  2303. */
  2304. if ((isci_device->status == isci_stopping)
  2305. || (isci_device->status == isci_stopped)
  2306. )
  2307. status = SAS_DEVICE_UNKNOWN;
  2308. else
  2309. status = SAS_ABORTED_TASK;
  2310. complete_to_host = isci_perform_aborted_io_completion;
  2311. /* This was an aborted request. */
  2312. spin_unlock(&request->state_lock);
  2313. break;
  2314. case aborting:
  2315. /* aborting means that the task management function tried and
  2316. * failed to abort the request. We need to note the request
  2317. * as SAS_TASK_UNDELIVERED, so that the scsi mid layer marks the
  2318. * target as down.
  2319. *
  2320. * Aborting also means an external thread is explicitly managing
  2321. * this request, so that we do not complete it up the stack.
  2322. */
  2323. request->complete_in_target = true;
  2324. response = SAS_TASK_UNDELIVERED;
  2325. if ((isci_device->status == isci_stopping) ||
  2326. (isci_device->status == isci_stopped))
  2327. /* The device has been /is being stopped. Note that
  2328. * we ignore the quiesce state, since we are
  2329. * concerned about the actual device state.
  2330. */
  2331. status = SAS_DEVICE_UNKNOWN;
  2332. else
  2333. status = SAS_PHY_DOWN;
  2334. complete_to_host = isci_perform_aborted_io_completion;
  2335. /* This was an aborted request. */
  2336. spin_unlock(&request->state_lock);
  2337. break;
  2338. case terminating:
  2339. /* This was an terminated request. This happens when
  2340. * the I/O is being terminated because of an action on
  2341. * the device (reset, tear down, etc.), and the I/O needs
  2342. * to be completed up the stack.
  2343. */
  2344. request->complete_in_target = true;
  2345. response = SAS_TASK_UNDELIVERED;
  2346. /* See if the device has been/is being stopped. Note
  2347. * that we ignore the quiesce state, since we are
  2348. * concerned about the actual device state.
  2349. */
  2350. if ((isci_device->status == isci_stopping) ||
  2351. (isci_device->status == isci_stopped))
  2352. status = SAS_DEVICE_UNKNOWN;
  2353. else
  2354. status = SAS_ABORTED_TASK;
  2355. complete_to_host = isci_perform_aborted_io_completion;
  2356. /* This was a terminated request. */
  2357. spin_unlock(&request->state_lock);
  2358. break;
  2359. default:
  2360. /* The request is done from an SCU HW perspective. */
  2361. request->status = completed;
  2362. spin_unlock(&request->state_lock);
  2363. /* This is an active request being completed from the core. */
  2364. switch (completion_status) {
  2365. case SCI_IO_FAILURE_RESPONSE_VALID:
  2366. dev_dbg(&isci_host->pdev->dev,
  2367. "%s: SCI_IO_FAILURE_RESPONSE_VALID (%p/%p)\n",
  2368. __func__,
  2369. request,
  2370. task);
  2371. if (sas_protocol_ata(task->task_proto)) {
  2372. resp_buf = &request->sci.stp.rsp;
  2373. isci_request_process_stp_response(task,
  2374. resp_buf);
  2375. } else if (SAS_PROTOCOL_SSP == task->task_proto) {
  2376. /* crack the iu response buffer. */
  2377. resp_iu = &request->sci.ssp.rsp;
  2378. isci_request_process_response_iu(task, resp_iu,
  2379. &isci_host->pdev->dev);
  2380. } else if (SAS_PROTOCOL_SMP == task->task_proto) {
  2381. dev_err(&isci_host->pdev->dev,
  2382. "%s: SCI_IO_FAILURE_RESPONSE_VALID: "
  2383. "SAS_PROTOCOL_SMP protocol\n",
  2384. __func__);
  2385. } else
  2386. dev_err(&isci_host->pdev->dev,
  2387. "%s: unknown protocol\n", __func__);
  2388. /* use the task status set in the task struct by the
  2389. * isci_request_process_response_iu call.
  2390. */
  2391. request->complete_in_target = true;
  2392. response = task->task_status.resp;
  2393. status = task->task_status.stat;
  2394. break;
  2395. case SCI_IO_SUCCESS:
  2396. case SCI_IO_SUCCESS_IO_DONE_EARLY:
  2397. response = SAS_TASK_COMPLETE;
  2398. status = SAM_STAT_GOOD;
  2399. request->complete_in_target = true;
  2400. if (task->task_proto == SAS_PROTOCOL_SMP) {
  2401. void *rsp = &request->sci.smp.rsp;
  2402. dev_dbg(&isci_host->pdev->dev,
  2403. "%s: SMP protocol completion\n",
  2404. __func__);
  2405. sg_copy_from_buffer(
  2406. &task->smp_task.smp_resp, 1,
  2407. rsp, sizeof(struct smp_resp));
  2408. } else if (completion_status
  2409. == SCI_IO_SUCCESS_IO_DONE_EARLY) {
  2410. /* This was an SSP / STP / SATA transfer.
  2411. * There is a possibility that less data than
  2412. * the maximum was transferred.
  2413. */
  2414. u32 transferred_length = sci_req_tx_bytes(&request->sci);
  2415. task->task_status.residual
  2416. = task->total_xfer_len - transferred_length;
  2417. /* If there were residual bytes, call this an
  2418. * underrun.
  2419. */
  2420. if (task->task_status.residual != 0)
  2421. status = SAS_DATA_UNDERRUN;
  2422. dev_dbg(&isci_host->pdev->dev,
  2423. "%s: SCI_IO_SUCCESS_IO_DONE_EARLY %d\n",
  2424. __func__,
  2425. status);
  2426. } else
  2427. dev_dbg(&isci_host->pdev->dev,
  2428. "%s: SCI_IO_SUCCESS\n",
  2429. __func__);
  2430. break;
  2431. case SCI_IO_FAILURE_TERMINATED:
  2432. dev_dbg(&isci_host->pdev->dev,
  2433. "%s: SCI_IO_FAILURE_TERMINATED (%p/%p)\n",
  2434. __func__,
  2435. request,
  2436. task);
  2437. /* The request was terminated explicitly. No handling
  2438. * is needed in the SCSI error handler path.
  2439. */
  2440. request->complete_in_target = true;
  2441. response = SAS_TASK_UNDELIVERED;
  2442. /* See if the device has been/is being stopped. Note
  2443. * that we ignore the quiesce state, since we are
  2444. * concerned about the actual device state.
  2445. */
  2446. if ((isci_device->status == isci_stopping) ||
  2447. (isci_device->status == isci_stopped))
  2448. status = SAS_DEVICE_UNKNOWN;
  2449. else
  2450. status = SAS_ABORTED_TASK;
  2451. complete_to_host = isci_perform_normal_io_completion;
  2452. break;
  2453. case SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR:
  2454. isci_request_handle_controller_specific_errors(
  2455. isci_device, request, task, &response, &status,
  2456. &complete_to_host);
  2457. break;
  2458. case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
  2459. /* This is a special case, in that the I/O completion
  2460. * is telling us that the device needs a reset.
  2461. * In order for the device reset condition to be
  2462. * noticed, the I/O has to be handled in the error
  2463. * handler. Set the reset flag and cause the
  2464. * SCSI error thread to be scheduled.
  2465. */
  2466. spin_lock_irqsave(&task->task_state_lock, task_flags);
  2467. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  2468. spin_unlock_irqrestore(&task->task_state_lock, task_flags);
  2469. /* Fail the I/O. */
  2470. response = SAS_TASK_UNDELIVERED;
  2471. status = SAM_STAT_TASK_ABORTED;
  2472. complete_to_host = isci_perform_error_io_completion;
  2473. request->complete_in_target = false;
  2474. break;
  2475. default:
  2476. /* Catch any otherwise unhandled error codes here. */
  2477. dev_warn(&isci_host->pdev->dev,
  2478. "%s: invalid completion code: 0x%x - "
  2479. "isci_request = %p\n",
  2480. __func__, completion_status, request);
  2481. response = SAS_TASK_UNDELIVERED;
  2482. /* See if the device has been/is being stopped. Note
  2483. * that we ignore the quiesce state, since we are
  2484. * concerned about the actual device state.
  2485. */
  2486. if ((isci_device->status == isci_stopping) ||
  2487. (isci_device->status == isci_stopped))
  2488. status = SAS_DEVICE_UNKNOWN;
  2489. else
  2490. status = SAS_ABORTED_TASK;
  2491. complete_to_host = isci_perform_error_io_completion;
  2492. request->complete_in_target = false;
  2493. break;
  2494. }
  2495. break;
  2496. }
  2497. isci_request_unmap_sgl(request, isci_host->pdev);
  2498. /* Put the completed request on the correct list */
  2499. isci_task_save_for_upper_layer_completion(isci_host, request, response,
  2500. status, complete_to_host
  2501. );
  2502. /* complete the io request to the core. */
  2503. scic_controller_complete_io(&isci_host->sci,
  2504. &isci_device->sci,
  2505. &request->sci);
  2506. /* set terminated handle so it cannot be completed or
  2507. * terminated again, and to cause any calls into abort
  2508. * task to recognize the already completed case.
  2509. */
  2510. request->terminated = true;
  2511. isci_host_can_dequeue(isci_host, 1);
  2512. }
  2513. /**
  2514. * scic_sds_request_initial_state_enter() -
  2515. * @object: This parameter specifies the base object for which the state
  2516. * transition is occurring.
  2517. *
  2518. * This method implements the actions taken when entering the
  2519. * SCI_BASE_REQUEST_STATE_INITIAL state. This state is entered when the initial
  2520. * base request is constructed. Entry into the initial state sets all handlers
  2521. * for the io request object to their default handlers. none
  2522. */
  2523. static void scic_sds_request_initial_state_enter(void *object)
  2524. {
  2525. struct scic_sds_request *sci_req = object;
  2526. SET_STATE_HANDLER(
  2527. sci_req,
  2528. scic_sds_request_state_handler_table,
  2529. SCI_BASE_REQUEST_STATE_INITIAL
  2530. );
  2531. }
  2532. /**
  2533. * scic_sds_request_constructed_state_enter() -
  2534. * @object: The io request object that is to enter the constructed state.
  2535. *
  2536. * This method implements the actions taken when entering the
  2537. * SCI_BASE_REQUEST_STATE_CONSTRUCTED state. The method sets the state handlers
  2538. * for the the constructed state. none
  2539. */
  2540. static void scic_sds_request_constructed_state_enter(void *object)
  2541. {
  2542. struct scic_sds_request *sci_req = object;
  2543. SET_STATE_HANDLER(
  2544. sci_req,
  2545. scic_sds_request_state_handler_table,
  2546. SCI_BASE_REQUEST_STATE_CONSTRUCTED
  2547. );
  2548. }
  2549. static void scic_sds_request_started_state_enter(void *object)
  2550. {
  2551. struct scic_sds_request *sci_req = object;
  2552. struct sci_base_state_machine *sm = &sci_req->state_machine;
  2553. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  2554. struct domain_device *dev = sci_dev_to_domain(sci_req->target_device);
  2555. struct sas_task *task;
  2556. /* XXX as hch said always creating an internal sas_task for tmf
  2557. * requests would simplify the driver
  2558. */
  2559. task = ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL;
  2560. SET_STATE_HANDLER(
  2561. sci_req,
  2562. scic_sds_request_state_handler_table,
  2563. SCI_BASE_REQUEST_STATE_STARTED
  2564. );
  2565. /* all unaccelerated request types (non ssp or ncq) handled with
  2566. * substates
  2567. */
  2568. if (!task && dev->dev_type == SAS_END_DEV) {
  2569. sci_base_state_machine_change_state(sm,
  2570. SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION);
  2571. } else if (!task &&
  2572. (isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_high ||
  2573. isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_low)) {
  2574. sci_base_state_machine_change_state(sm,
  2575. SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE);
  2576. } else if (task && task->task_proto == SAS_PROTOCOL_SMP) {
  2577. sci_base_state_machine_change_state(sm,
  2578. SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE);
  2579. } else if (task && sas_protocol_ata(task->task_proto) &&
  2580. !task->ata_task.use_ncq) {
  2581. u32 state;
  2582. if (task->data_dir == DMA_NONE)
  2583. state = SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE;
  2584. else if (task->ata_task.dma_xfer)
  2585. state = SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE;
  2586. else /* PIO */
  2587. state = SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE;
  2588. sci_base_state_machine_change_state(sm, state);
  2589. }
  2590. }
  2591. /**
  2592. * scic_sds_request_completed_state_enter() -
  2593. * @object: This parameter specifies the base object for which the state
  2594. * transition is occurring. This object is cast into a SCIC_SDS_IO_REQUEST
  2595. * object.
  2596. *
  2597. * This method implements the actions taken when entering the
  2598. * SCI_BASE_REQUEST_STATE_COMPLETED state. This state is entered when the
  2599. * SCIC_SDS_IO_REQUEST has completed. The method will decode the request
  2600. * completion status and convert it to an enum sci_status to return in the
  2601. * completion callback function. none
  2602. */
  2603. static void scic_sds_request_completed_state_enter(void *object)
  2604. {
  2605. struct scic_sds_request *sci_req = object;
  2606. struct scic_sds_controller *scic =
  2607. scic_sds_request_get_controller(sci_req);
  2608. struct isci_host *ihost = scic_to_ihost(scic);
  2609. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  2610. SET_STATE_HANDLER(sci_req,
  2611. scic_sds_request_state_handler_table,
  2612. SCI_BASE_REQUEST_STATE_COMPLETED);
  2613. /* Tell the SCI_USER that the IO request is complete */
  2614. if (sci_req->is_task_management_request == false)
  2615. isci_request_io_request_complete(ihost, ireq,
  2616. sci_req->sci_status);
  2617. else
  2618. isci_task_request_complete(ihost, ireq, sci_req->sci_status);
  2619. }
  2620. /**
  2621. * scic_sds_request_aborting_state_enter() -
  2622. * @object: This parameter specifies the base object for which the state
  2623. * transition is occurring. This object is cast into a SCIC_SDS_IO_REQUEST
  2624. * object.
  2625. *
  2626. * This method implements the actions taken when entering the
  2627. * SCI_BASE_REQUEST_STATE_ABORTING state. none
  2628. */
  2629. static void scic_sds_request_aborting_state_enter(void *object)
  2630. {
  2631. struct scic_sds_request *sci_req = object;
  2632. /* Setting the abort bit in the Task Context is required by the silicon. */
  2633. sci_req->task_context_buffer->abort = 1;
  2634. SET_STATE_HANDLER(
  2635. sci_req,
  2636. scic_sds_request_state_handler_table,
  2637. SCI_BASE_REQUEST_STATE_ABORTING
  2638. );
  2639. }
  2640. /**
  2641. * scic_sds_request_final_state_enter() -
  2642. * @object: This parameter specifies the base object for which the state
  2643. * transition is occurring. This is cast into a SCIC_SDS_IO_REQUEST object.
  2644. *
  2645. * This method implements the actions taken when entering the
  2646. * SCI_BASE_REQUEST_STATE_FINAL state. The only action required is to put the
  2647. * state handlers in place. none
  2648. */
  2649. static void scic_sds_request_final_state_enter(void *object)
  2650. {
  2651. struct scic_sds_request *sci_req = object;
  2652. SET_STATE_HANDLER(
  2653. sci_req,
  2654. scic_sds_request_state_handler_table,
  2655. SCI_BASE_REQUEST_STATE_FINAL
  2656. );
  2657. }
  2658. static void scic_sds_io_request_started_task_mgmt_await_tc_completion_substate_enter(
  2659. void *object)
  2660. {
  2661. struct scic_sds_request *sci_req = object;
  2662. SET_STATE_HANDLER(
  2663. sci_req,
  2664. scic_sds_request_state_handler_table,
  2665. SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION
  2666. );
  2667. }
  2668. static void scic_sds_io_request_started_task_mgmt_await_task_response_substate_enter(
  2669. void *object)
  2670. {
  2671. struct scic_sds_request *sci_req = object;
  2672. SET_STATE_HANDLER(
  2673. sci_req,
  2674. scic_sds_request_state_handler_table,
  2675. SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE
  2676. );
  2677. }
  2678. static void scic_sds_smp_request_started_await_response_substate_enter(void *object)
  2679. {
  2680. struct scic_sds_request *sci_req = object;
  2681. SET_STATE_HANDLER(
  2682. sci_req,
  2683. scic_sds_request_state_handler_table,
  2684. SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE
  2685. );
  2686. }
  2687. static void scic_sds_smp_request_started_await_tc_completion_substate_enter(void *object)
  2688. {
  2689. struct scic_sds_request *sci_req = object;
  2690. SET_STATE_HANDLER(
  2691. sci_req,
  2692. scic_sds_request_state_handler_table,
  2693. SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION
  2694. );
  2695. }
  2696. static void scic_sds_stp_request_started_non_data_await_h2d_completion_enter(
  2697. void *object)
  2698. {
  2699. struct scic_sds_request *sci_req = object;
  2700. SET_STATE_HANDLER(
  2701. sci_req,
  2702. scic_sds_request_state_handler_table,
  2703. SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE
  2704. );
  2705. scic_sds_remote_device_set_working_request(
  2706. sci_req->target_device, sci_req
  2707. );
  2708. }
  2709. static void scic_sds_stp_request_started_non_data_await_d2h_enter(void *object)
  2710. {
  2711. struct scic_sds_request *sci_req = object;
  2712. SET_STATE_HANDLER(
  2713. sci_req,
  2714. scic_sds_request_state_handler_table,
  2715. SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE
  2716. );
  2717. }
  2718. static void scic_sds_stp_request_started_pio_await_h2d_completion_enter(
  2719. void *object)
  2720. {
  2721. struct scic_sds_request *sci_req = object;
  2722. SET_STATE_HANDLER(
  2723. sci_req,
  2724. scic_sds_request_state_handler_table,
  2725. SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE
  2726. );
  2727. scic_sds_remote_device_set_working_request(
  2728. sci_req->target_device, sci_req);
  2729. }
  2730. static void scic_sds_stp_request_started_pio_await_frame_enter(void *object)
  2731. {
  2732. struct scic_sds_request *sci_req = object;
  2733. SET_STATE_HANDLER(
  2734. sci_req,
  2735. scic_sds_request_state_handler_table,
  2736. SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE
  2737. );
  2738. }
  2739. static void scic_sds_stp_request_started_pio_data_in_await_data_enter(
  2740. void *object)
  2741. {
  2742. struct scic_sds_request *sci_req = object;
  2743. SET_STATE_HANDLER(
  2744. sci_req,
  2745. scic_sds_request_state_handler_table,
  2746. SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE
  2747. );
  2748. }
  2749. static void scic_sds_stp_request_started_pio_data_out_transmit_data_enter(
  2750. void *object)
  2751. {
  2752. struct scic_sds_request *sci_req = object;
  2753. SET_STATE_HANDLER(
  2754. sci_req,
  2755. scic_sds_request_state_handler_table,
  2756. SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE
  2757. );
  2758. }
  2759. static void scic_sds_stp_request_started_udma_await_tc_completion_enter(
  2760. void *object)
  2761. {
  2762. struct scic_sds_request *sci_req = object;
  2763. SET_STATE_HANDLER(
  2764. sci_req,
  2765. scic_sds_request_state_handler_table,
  2766. SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE
  2767. );
  2768. }
  2769. /**
  2770. *
  2771. *
  2772. * This state is entered when there is an TC completion failure. The hardware
  2773. * received an unexpected condition while processing the IO request and now
  2774. * will UF the D2H register FIS to complete the IO.
  2775. */
  2776. static void scic_sds_stp_request_started_udma_await_d2h_reg_fis_enter(
  2777. void *object)
  2778. {
  2779. struct scic_sds_request *sci_req = object;
  2780. SET_STATE_HANDLER(
  2781. sci_req,
  2782. scic_sds_request_state_handler_table,
  2783. SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE
  2784. );
  2785. }
  2786. static void scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter(
  2787. void *object)
  2788. {
  2789. struct scic_sds_request *sci_req = object;
  2790. SET_STATE_HANDLER(
  2791. sci_req,
  2792. scic_sds_request_state_handler_table,
  2793. SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE
  2794. );
  2795. scic_sds_remote_device_set_working_request(
  2796. sci_req->target_device, sci_req
  2797. );
  2798. }
  2799. static void scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter(
  2800. void *object)
  2801. {
  2802. struct scic_sds_request *sci_req = object;
  2803. struct scu_task_context *task_context;
  2804. struct host_to_dev_fis *h2d_fis;
  2805. enum sci_status status;
  2806. /* Clear the SRST bit */
  2807. h2d_fis = &sci_req->stp.cmd;
  2808. h2d_fis->control = 0;
  2809. /* Clear the TC control bit */
  2810. task_context = scic_sds_controller_get_task_context_buffer(
  2811. sci_req->owning_controller, sci_req->io_tag);
  2812. task_context->control_frame = 0;
  2813. status = scic_controller_continue_io(sci_req);
  2814. if (status == SCI_SUCCESS) {
  2815. SET_STATE_HANDLER(
  2816. sci_req,
  2817. scic_sds_request_state_handler_table,
  2818. SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE
  2819. );
  2820. }
  2821. }
  2822. static void scic_sds_stp_request_started_soft_reset_await_d2h_response_enter(
  2823. void *object)
  2824. {
  2825. struct scic_sds_request *sci_req = object;
  2826. SET_STATE_HANDLER(
  2827. sci_req,
  2828. scic_sds_request_state_handler_table,
  2829. SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE
  2830. );
  2831. }
  2832. static const struct sci_base_state scic_sds_request_state_table[] = {
  2833. [SCI_BASE_REQUEST_STATE_INITIAL] = {
  2834. .enter_state = scic_sds_request_initial_state_enter,
  2835. },
  2836. [SCI_BASE_REQUEST_STATE_CONSTRUCTED] = {
  2837. .enter_state = scic_sds_request_constructed_state_enter,
  2838. },
  2839. [SCI_BASE_REQUEST_STATE_STARTED] = {
  2840. .enter_state = scic_sds_request_started_state_enter,
  2841. },
  2842. [SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE] = {
  2843. .enter_state = scic_sds_stp_request_started_non_data_await_h2d_completion_enter,
  2844. },
  2845. [SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE] = {
  2846. .enter_state = scic_sds_stp_request_started_non_data_await_d2h_enter,
  2847. },
  2848. [SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE] = {
  2849. .enter_state = scic_sds_stp_request_started_pio_await_h2d_completion_enter,
  2850. },
  2851. [SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE] = {
  2852. .enter_state = scic_sds_stp_request_started_pio_await_frame_enter,
  2853. },
  2854. [SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE] = {
  2855. .enter_state = scic_sds_stp_request_started_pio_data_in_await_data_enter,
  2856. },
  2857. [SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE] = {
  2858. .enter_state = scic_sds_stp_request_started_pio_data_out_transmit_data_enter,
  2859. },
  2860. [SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE] = {
  2861. .enter_state = scic_sds_stp_request_started_udma_await_tc_completion_enter,
  2862. },
  2863. [SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE] = {
  2864. .enter_state = scic_sds_stp_request_started_udma_await_d2h_reg_fis_enter,
  2865. },
  2866. [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE] = {
  2867. .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter,
  2868. },
  2869. [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE] = {
  2870. .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter,
  2871. },
  2872. [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE] = {
  2873. .enter_state = scic_sds_stp_request_started_soft_reset_await_d2h_response_enter,
  2874. },
  2875. [SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION] = {
  2876. .enter_state = scic_sds_io_request_started_task_mgmt_await_tc_completion_substate_enter,
  2877. },
  2878. [SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_RESPONSE] = {
  2879. .enter_state = scic_sds_io_request_started_task_mgmt_await_task_response_substate_enter,
  2880. },
  2881. [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE] = {
  2882. .enter_state = scic_sds_smp_request_started_await_response_substate_enter,
  2883. },
  2884. [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION] = {
  2885. .enter_state = scic_sds_smp_request_started_await_tc_completion_substate_enter,
  2886. },
  2887. [SCI_BASE_REQUEST_STATE_COMPLETED] = {
  2888. .enter_state = scic_sds_request_completed_state_enter,
  2889. },
  2890. [SCI_BASE_REQUEST_STATE_ABORTING] = {
  2891. .enter_state = scic_sds_request_aborting_state_enter,
  2892. },
  2893. [SCI_BASE_REQUEST_STATE_FINAL] = {
  2894. .enter_state = scic_sds_request_final_state_enter,
  2895. },
  2896. };
  2897. static void scic_sds_general_request_construct(struct scic_sds_controller *scic,
  2898. struct scic_sds_remote_device *sci_dev,
  2899. u16 io_tag, struct scic_sds_request *sci_req)
  2900. {
  2901. sci_base_state_machine_construct(&sci_req->state_machine, sci_req,
  2902. scic_sds_request_state_table, SCI_BASE_REQUEST_STATE_INITIAL);
  2903. sci_base_state_machine_start(&sci_req->state_machine);
  2904. sci_req->io_tag = io_tag;
  2905. sci_req->owning_controller = scic;
  2906. sci_req->target_device = sci_dev;
  2907. sci_req->protocol = SCIC_NO_PROTOCOL;
  2908. sci_req->saved_rx_frame_index = SCU_INVALID_FRAME_INDEX;
  2909. sci_req->device_sequence = scic_sds_remote_device_get_sequence(sci_dev);
  2910. sci_req->sci_status = SCI_SUCCESS;
  2911. sci_req->scu_status = 0;
  2912. sci_req->post_context = 0xFFFFFFFF;
  2913. sci_req->is_task_management_request = false;
  2914. if (io_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
  2915. sci_req->was_tag_assigned_by_user = false;
  2916. sci_req->task_context_buffer = &sci_req->tc;
  2917. } else {
  2918. sci_req->was_tag_assigned_by_user = true;
  2919. sci_req->task_context_buffer =
  2920. scic_sds_controller_get_task_context_buffer(scic, io_tag);
  2921. }
  2922. }
  2923. static enum sci_status
  2924. scic_io_request_construct(struct scic_sds_controller *scic,
  2925. struct scic_sds_remote_device *sci_dev,
  2926. u16 io_tag, struct scic_sds_request *sci_req)
  2927. {
  2928. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  2929. enum sci_status status = SCI_SUCCESS;
  2930. /* Build the common part of the request */
  2931. scic_sds_general_request_construct(scic, sci_dev, io_tag, sci_req);
  2932. if (sci_dev->rnc.remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
  2933. return SCI_FAILURE_INVALID_REMOTE_DEVICE;
  2934. if (dev->dev_type == SAS_END_DEV)
  2935. /* pass */;
  2936. else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
  2937. memset(&sci_req->stp.cmd, 0, sizeof(sci_req->stp.cmd));
  2938. else if (dev_is_expander(dev))
  2939. memset(&sci_req->smp.cmd, 0, sizeof(sci_req->smp.cmd));
  2940. else
  2941. return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  2942. memset(sci_req->task_context_buffer, 0,
  2943. offsetof(struct scu_task_context, sgl_pair_ab));
  2944. return status;
  2945. }
  2946. enum sci_status scic_task_request_construct(struct scic_sds_controller *scic,
  2947. struct scic_sds_remote_device *sci_dev,
  2948. u16 io_tag, struct scic_sds_request *sci_req)
  2949. {
  2950. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  2951. enum sci_status status = SCI_SUCCESS;
  2952. /* Build the common part of the request */
  2953. scic_sds_general_request_construct(scic, sci_dev, io_tag, sci_req);
  2954. if (dev->dev_type == SAS_END_DEV ||
  2955. dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
  2956. sci_req->is_task_management_request = true;
  2957. memset(sci_req->task_context_buffer, 0, sizeof(struct scu_task_context));
  2958. } else
  2959. status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  2960. return status;
  2961. }
  2962. static enum sci_status isci_request_ssp_request_construct(
  2963. struct isci_request *request)
  2964. {
  2965. enum sci_status status;
  2966. dev_dbg(&request->isci_host->pdev->dev,
  2967. "%s: request = %p\n",
  2968. __func__,
  2969. request);
  2970. status = scic_io_request_construct_basic_ssp(&request->sci);
  2971. return status;
  2972. }
  2973. static enum sci_status isci_request_stp_request_construct(
  2974. struct isci_request *request)
  2975. {
  2976. struct sas_task *task = isci_request_access_task(request);
  2977. enum sci_status status;
  2978. struct host_to_dev_fis *register_fis;
  2979. dev_dbg(&request->isci_host->pdev->dev,
  2980. "%s: request = %p\n",
  2981. __func__,
  2982. request);
  2983. /* Get the host_to_dev_fis from the core and copy
  2984. * the fis from the task into it.
  2985. */
  2986. register_fis = isci_sata_task_to_fis_copy(task);
  2987. status = scic_io_request_construct_basic_sata(&request->sci);
  2988. /* Set the ncq tag in the fis, from the queue
  2989. * command in the task.
  2990. */
  2991. if (isci_sata_is_task_ncq(task)) {
  2992. isci_sata_set_ncq_tag(
  2993. register_fis,
  2994. task
  2995. );
  2996. }
  2997. return status;
  2998. }
  2999. /*
  3000. * This function will fill in the SCU Task Context for a SMP request. The
  3001. * following important settings are utilized: -# task_type ==
  3002. * SCU_TASK_TYPE_SMP. This simply indicates that a normal request type
  3003. * (i.e. non-raw frame) is being utilized to perform task management. -#
  3004. * control_frame == 1. This ensures that the proper endianess is set so
  3005. * that the bytes are transmitted in the right order for a smp request frame.
  3006. * @sci_req: This parameter specifies the smp request object being
  3007. * constructed.
  3008. *
  3009. */
  3010. static void
  3011. scu_smp_request_construct_task_context(struct scic_sds_request *sci_req,
  3012. struct smp_req *smp_req)
  3013. {
  3014. dma_addr_t dma_addr;
  3015. struct scic_sds_controller *scic;
  3016. struct scic_sds_remote_device *sci_dev;
  3017. struct scic_sds_port *sci_port;
  3018. struct scu_task_context *task_context;
  3019. ssize_t word_cnt = sizeof(struct smp_req) / sizeof(u32);
  3020. /* byte swap the smp request. */
  3021. sci_swab32_cpy(&sci_req->smp.cmd, smp_req,
  3022. word_cnt);
  3023. task_context = scic_sds_request_get_task_context(sci_req);
  3024. scic = scic_sds_request_get_controller(sci_req);
  3025. sci_dev = scic_sds_request_get_device(sci_req);
  3026. sci_port = scic_sds_request_get_port(sci_req);
  3027. /*
  3028. * Fill in the TC with the its required data
  3029. * 00h
  3030. */
  3031. task_context->priority = 0;
  3032. task_context->initiator_request = 1;
  3033. task_context->connection_rate = sci_dev->connection_rate;
  3034. task_context->protocol_engine_index =
  3035. scic_sds_controller_get_protocol_engine_group(scic);
  3036. task_context->logical_port_index = scic_sds_port_get_index(sci_port);
  3037. task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SMP;
  3038. task_context->abort = 0;
  3039. task_context->valid = SCU_TASK_CONTEXT_VALID;
  3040. task_context->context_type = SCU_TASK_CONTEXT_TYPE;
  3041. /* 04h */
  3042. task_context->remote_node_index = sci_dev->rnc.remote_node_index;
  3043. task_context->command_code = 0;
  3044. task_context->task_type = SCU_TASK_TYPE_SMP_REQUEST;
  3045. /* 08h */
  3046. task_context->link_layer_control = 0;
  3047. task_context->do_not_dma_ssp_good_response = 1;
  3048. task_context->strict_ordering = 0;
  3049. task_context->control_frame = 1;
  3050. task_context->timeout_enable = 0;
  3051. task_context->block_guard_enable = 0;
  3052. /* 0ch */
  3053. task_context->address_modifier = 0;
  3054. /* 10h */
  3055. task_context->ssp_command_iu_length = smp_req->req_len;
  3056. /* 14h */
  3057. task_context->transfer_length_bytes = 0;
  3058. /*
  3059. * 18h ~ 30h, protocol specific
  3060. * since commandIU has been build by framework at this point, we just
  3061. * copy the frist DWord from command IU to this location. */
  3062. memcpy(&task_context->type.smp, &sci_req->smp.cmd, sizeof(u32));
  3063. /*
  3064. * 40h
  3065. * "For SMP you could program it to zero. We would prefer that way
  3066. * so that done code will be consistent." - Venki
  3067. */
  3068. task_context->task_phase = 0;
  3069. if (sci_req->was_tag_assigned_by_user) {
  3070. /*
  3071. * Build the task context now since we have already read
  3072. * the data
  3073. */
  3074. sci_req->post_context =
  3075. (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  3076. (scic_sds_controller_get_protocol_engine_group(scic) <<
  3077. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  3078. (scic_sds_port_get_index(sci_port) <<
  3079. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
  3080. scic_sds_io_tag_get_index(sci_req->io_tag));
  3081. } else {
  3082. /*
  3083. * Build the task context now since we have already read
  3084. * the data.
  3085. * I/O tag index is not assigned because we have to wait
  3086. * until we get a TCi.
  3087. */
  3088. sci_req->post_context =
  3089. (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  3090. (scic_sds_controller_get_protocol_engine_group(scic) <<
  3091. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  3092. (scic_sds_port_get_index(sci_port) <<
  3093. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
  3094. }
  3095. /*
  3096. * Copy the physical address for the command buffer to the SCU Task
  3097. * Context command buffer should not contain command header.
  3098. */
  3099. dma_addr = scic_io_request_get_dma_addr(sci_req,
  3100. ((char *) &sci_req->smp.cmd) +
  3101. sizeof(u32));
  3102. task_context->command_iu_upper = upper_32_bits(dma_addr);
  3103. task_context->command_iu_lower = lower_32_bits(dma_addr);
  3104. /* SMP response comes as UF, so no need to set response IU address. */
  3105. task_context->response_iu_upper = 0;
  3106. task_context->response_iu_lower = 0;
  3107. }
  3108. static enum sci_status scic_io_request_construct_smp(struct scic_sds_request *sci_req)
  3109. {
  3110. struct smp_req *smp_req = kmalloc(sizeof(*smp_req), GFP_KERNEL);
  3111. if (!smp_req)
  3112. return SCI_FAILURE_INSUFFICIENT_RESOURCES;
  3113. sci_req->protocol = SCIC_SMP_PROTOCOL;
  3114. /* Construct the SMP SCU Task Context */
  3115. memcpy(smp_req, &sci_req->smp.cmd, sizeof(*smp_req));
  3116. /*
  3117. * Look at the SMP requests' header fields; for certain SAS 1.x SMP
  3118. * functions under SAS 2.0, a zero request length really indicates
  3119. * a non-zero default length. */
  3120. if (smp_req->req_len == 0) {
  3121. switch (smp_req->func) {
  3122. case SMP_DISCOVER:
  3123. case SMP_REPORT_PHY_ERR_LOG:
  3124. case SMP_REPORT_PHY_SATA:
  3125. case SMP_REPORT_ROUTE_INFO:
  3126. smp_req->req_len = 2;
  3127. break;
  3128. case SMP_CONF_ROUTE_INFO:
  3129. case SMP_PHY_CONTROL:
  3130. case SMP_PHY_TEST_FUNCTION:
  3131. smp_req->req_len = 9;
  3132. break;
  3133. /* Default - zero is a valid default for 2.0. */
  3134. }
  3135. }
  3136. scu_smp_request_construct_task_context(sci_req, smp_req);
  3137. sci_base_state_machine_change_state(&sci_req->state_machine,
  3138. SCI_BASE_REQUEST_STATE_CONSTRUCTED);
  3139. kfree(smp_req);
  3140. return SCI_SUCCESS;
  3141. }
  3142. /*
  3143. * isci_smp_request_build() - This function builds the smp request.
  3144. * @ireq: This parameter points to the isci_request allocated in the
  3145. * request construct function.
  3146. *
  3147. * SCI_SUCCESS on successfull completion, or specific failure code.
  3148. */
  3149. static enum sci_status isci_smp_request_build(struct isci_request *ireq)
  3150. {
  3151. enum sci_status status = SCI_FAILURE;
  3152. struct sas_task *task = isci_request_access_task(ireq);
  3153. struct scic_sds_request *sci_req = &ireq->sci;
  3154. dev_dbg(&ireq->isci_host->pdev->dev,
  3155. "%s: request = %p\n", __func__, ireq);
  3156. dev_dbg(&ireq->isci_host->pdev->dev,
  3157. "%s: smp_req len = %d\n",
  3158. __func__,
  3159. task->smp_task.smp_req.length);
  3160. /* copy the smp_command to the address; */
  3161. sg_copy_to_buffer(&task->smp_task.smp_req, 1,
  3162. &sci_req->smp.cmd,
  3163. sizeof(struct smp_req));
  3164. status = scic_io_request_construct_smp(sci_req);
  3165. if (status != SCI_SUCCESS)
  3166. dev_warn(&ireq->isci_host->pdev->dev,
  3167. "%s: failed with status = %d\n",
  3168. __func__,
  3169. status);
  3170. return status;
  3171. }
  3172. /**
  3173. * isci_io_request_build() - This function builds the io request object.
  3174. * @isci_host: This parameter specifies the ISCI host object
  3175. * @request: This parameter points to the isci_request object allocated in the
  3176. * request construct function.
  3177. * @sci_device: This parameter is the handle for the sci core's remote device
  3178. * object that is the destination for this request.
  3179. *
  3180. * SCI_SUCCESS on successfull completion, or specific failure code.
  3181. */
  3182. static enum sci_status isci_io_request_build(
  3183. struct isci_host *isci_host,
  3184. struct isci_request *request,
  3185. struct isci_remote_device *isci_device)
  3186. {
  3187. enum sci_status status = SCI_SUCCESS;
  3188. struct sas_task *task = isci_request_access_task(request);
  3189. struct scic_sds_remote_device *sci_device = &isci_device->sci;
  3190. dev_dbg(&isci_host->pdev->dev,
  3191. "%s: isci_device = 0x%p; request = %p, "
  3192. "num_scatter = %d\n",
  3193. __func__,
  3194. isci_device,
  3195. request,
  3196. task->num_scatter);
  3197. /* map the sgl addresses, if present.
  3198. * libata does the mapping for sata devices
  3199. * before we get the request.
  3200. */
  3201. if (task->num_scatter &&
  3202. !sas_protocol_ata(task->task_proto) &&
  3203. !(SAS_PROTOCOL_SMP & task->task_proto)) {
  3204. request->num_sg_entries = dma_map_sg(
  3205. &isci_host->pdev->dev,
  3206. task->scatter,
  3207. task->num_scatter,
  3208. task->data_dir
  3209. );
  3210. if (request->num_sg_entries == 0)
  3211. return SCI_FAILURE_INSUFFICIENT_RESOURCES;
  3212. }
  3213. /* build the common request object. For now,
  3214. * we will let the core allocate the IO tag.
  3215. */
  3216. status = scic_io_request_construct(&isci_host->sci, sci_device,
  3217. SCI_CONTROLLER_INVALID_IO_TAG,
  3218. &request->sci);
  3219. if (status != SCI_SUCCESS) {
  3220. dev_warn(&isci_host->pdev->dev,
  3221. "%s: failed request construct\n",
  3222. __func__);
  3223. return SCI_FAILURE;
  3224. }
  3225. switch (task->task_proto) {
  3226. case SAS_PROTOCOL_SMP:
  3227. status = isci_smp_request_build(request);
  3228. break;
  3229. case SAS_PROTOCOL_SSP:
  3230. status = isci_request_ssp_request_construct(request);
  3231. break;
  3232. case SAS_PROTOCOL_SATA:
  3233. case SAS_PROTOCOL_STP:
  3234. case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
  3235. status = isci_request_stp_request_construct(request);
  3236. break;
  3237. default:
  3238. dev_warn(&isci_host->pdev->dev,
  3239. "%s: unknown protocol\n", __func__);
  3240. return SCI_FAILURE;
  3241. }
  3242. return SCI_SUCCESS;
  3243. }
  3244. /**
  3245. * isci_request_alloc_core() - This function gets the request object from the
  3246. * isci_host dma cache.
  3247. * @isci_host: This parameter specifies the ISCI host object
  3248. * @isci_request: This parameter will contain the pointer to the new
  3249. * isci_request object.
  3250. * @isci_device: This parameter is the pointer to the isci remote device object
  3251. * that is the destination for this request.
  3252. * @gfp_flags: This parameter specifies the os allocation flags.
  3253. *
  3254. * SCI_SUCCESS on successfull completion, or specific failure code.
  3255. */
  3256. static int isci_request_alloc_core(
  3257. struct isci_host *isci_host,
  3258. struct isci_request **isci_request,
  3259. struct isci_remote_device *isci_device,
  3260. gfp_t gfp_flags)
  3261. {
  3262. int ret = 0;
  3263. dma_addr_t handle;
  3264. struct isci_request *request;
  3265. /* get pointer to dma memory. This actually points
  3266. * to both the isci_remote_device object and the
  3267. * sci object. The isci object is at the beginning
  3268. * of the memory allocated here.
  3269. */
  3270. request = dma_pool_alloc(isci_host->dma_pool, gfp_flags, &handle);
  3271. if (!request) {
  3272. dev_warn(&isci_host->pdev->dev,
  3273. "%s: dma_pool_alloc returned NULL\n", __func__);
  3274. return -ENOMEM;
  3275. }
  3276. /* initialize the request object. */
  3277. spin_lock_init(&request->state_lock);
  3278. request->request_daddr = handle;
  3279. request->isci_host = isci_host;
  3280. request->isci_device = isci_device;
  3281. request->io_request_completion = NULL;
  3282. request->terminated = false;
  3283. request->num_sg_entries = 0;
  3284. request->complete_in_target = false;
  3285. INIT_LIST_HEAD(&request->completed_node);
  3286. INIT_LIST_HEAD(&request->dev_node);
  3287. *isci_request = request;
  3288. isci_request_change_state(request, allocated);
  3289. return ret;
  3290. }
  3291. static int isci_request_alloc_io(
  3292. struct isci_host *isci_host,
  3293. struct sas_task *task,
  3294. struct isci_request **isci_request,
  3295. struct isci_remote_device *isci_device,
  3296. gfp_t gfp_flags)
  3297. {
  3298. int retval = isci_request_alloc_core(isci_host, isci_request,
  3299. isci_device, gfp_flags);
  3300. if (!retval) {
  3301. (*isci_request)->ttype_ptr.io_task_ptr = task;
  3302. (*isci_request)->ttype = io_task;
  3303. task->lldd_task = *isci_request;
  3304. }
  3305. return retval;
  3306. }
  3307. /**
  3308. * isci_request_alloc_tmf() - This function gets the request object from the
  3309. * isci_host dma cache and initializes the relevant fields as a sas_task.
  3310. * @isci_host: This parameter specifies the ISCI host object
  3311. * @sas_task: This parameter is the task struct from the upper layer driver.
  3312. * @isci_request: This parameter will contain the pointer to the new
  3313. * isci_request object.
  3314. * @isci_device: This parameter is the pointer to the isci remote device object
  3315. * that is the destination for this request.
  3316. * @gfp_flags: This parameter specifies the os allocation flags.
  3317. *
  3318. * SCI_SUCCESS on successfull completion, or specific failure code.
  3319. */
  3320. int isci_request_alloc_tmf(
  3321. struct isci_host *isci_host,
  3322. struct isci_tmf *isci_tmf,
  3323. struct isci_request **isci_request,
  3324. struct isci_remote_device *isci_device,
  3325. gfp_t gfp_flags)
  3326. {
  3327. int retval = isci_request_alloc_core(isci_host, isci_request,
  3328. isci_device, gfp_flags);
  3329. if (!retval) {
  3330. (*isci_request)->ttype_ptr.tmf_task_ptr = isci_tmf;
  3331. (*isci_request)->ttype = tmf_task;
  3332. }
  3333. return retval;
  3334. }
  3335. /**
  3336. * isci_request_execute() - This function allocates the isci_request object,
  3337. * all fills in some common fields.
  3338. * @isci_host: This parameter specifies the ISCI host object
  3339. * @sas_task: This parameter is the task struct from the upper layer driver.
  3340. * @isci_request: This parameter will contain the pointer to the new
  3341. * isci_request object.
  3342. * @gfp_flags: This parameter specifies the os allocation flags.
  3343. *
  3344. * SCI_SUCCESS on successfull completion, or specific failure code.
  3345. */
  3346. int isci_request_execute(
  3347. struct isci_host *isci_host,
  3348. struct sas_task *task,
  3349. struct isci_request **isci_request,
  3350. gfp_t gfp_flags)
  3351. {
  3352. int ret = 0;
  3353. struct scic_sds_remote_device *sci_device;
  3354. enum sci_status status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  3355. struct isci_remote_device *isci_device;
  3356. struct isci_request *request;
  3357. unsigned long flags;
  3358. isci_device = task->dev->lldd_dev;
  3359. sci_device = &isci_device->sci;
  3360. /* do common allocation and init of request object. */
  3361. ret = isci_request_alloc_io(
  3362. isci_host,
  3363. task,
  3364. &request,
  3365. isci_device,
  3366. gfp_flags
  3367. );
  3368. if (ret)
  3369. goto out;
  3370. status = isci_io_request_build(isci_host, request, isci_device);
  3371. if (status != SCI_SUCCESS) {
  3372. dev_warn(&isci_host->pdev->dev,
  3373. "%s: request_construct failed - status = 0x%x\n",
  3374. __func__,
  3375. status);
  3376. goto out;
  3377. }
  3378. spin_lock_irqsave(&isci_host->scic_lock, flags);
  3379. /* send the request, let the core assign the IO TAG. */
  3380. status = scic_controller_start_io(&isci_host->sci, sci_device,
  3381. &request->sci,
  3382. SCI_CONTROLLER_INVALID_IO_TAG);
  3383. if (status != SCI_SUCCESS &&
  3384. status != SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  3385. dev_warn(&isci_host->pdev->dev,
  3386. "%s: failed request start (0x%x)\n",
  3387. __func__, status);
  3388. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  3389. goto out;
  3390. }
  3391. /* Either I/O started OK, or the core has signaled that
  3392. * the device needs a target reset.
  3393. *
  3394. * In either case, hold onto the I/O for later.
  3395. *
  3396. * Update it's status and add it to the list in the
  3397. * remote device object.
  3398. */
  3399. isci_request_change_state(request, started);
  3400. list_add(&request->dev_node, &isci_device->reqs_in_process);
  3401. if (status == SCI_SUCCESS) {
  3402. /* Save the tag for possible task mgmt later. */
  3403. request->io_tag = request->sci.io_tag;
  3404. } else {
  3405. /* The request did not really start in the
  3406. * hardware, so clear the request handle
  3407. * here so no terminations will be done.
  3408. */
  3409. request->terminated = true;
  3410. }
  3411. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  3412. if (status ==
  3413. SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  3414. /* Signal libsas that we need the SCSI error
  3415. * handler thread to work on this I/O and that
  3416. * we want a device reset.
  3417. */
  3418. spin_lock_irqsave(&task->task_state_lock, flags);
  3419. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  3420. spin_unlock_irqrestore(&task->task_state_lock, flags);
  3421. /* Cause this task to be scheduled in the SCSI error
  3422. * handler thread.
  3423. */
  3424. isci_execpath_callback(isci_host, task,
  3425. sas_task_abort);
  3426. /* Change the status, since we are holding
  3427. * the I/O until it is managed by the SCSI
  3428. * error handler.
  3429. */
  3430. status = SCI_SUCCESS;
  3431. }
  3432. out:
  3433. if (status != SCI_SUCCESS) {
  3434. /* release dma memory on failure. */
  3435. isci_request_free(isci_host, request);
  3436. request = NULL;
  3437. ret = SCI_FAILURE;
  3438. }
  3439. *isci_request = request;
  3440. return ret;
  3441. }