request.c 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663
  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 "scu_completion_codes.h"
  59. #include "scu_event_codes.h"
  60. #include "sas.h"
  61. static struct scu_sgl_element_pair *to_sgl_element_pair(struct isci_request *ireq,
  62. int idx)
  63. {
  64. if (idx == 0)
  65. return &ireq->tc->sgl_pair_ab;
  66. else if (idx == 1)
  67. return &ireq->tc->sgl_pair_cd;
  68. else if (idx < 0)
  69. return NULL;
  70. else
  71. return &ireq->sg_table[idx - 2];
  72. }
  73. static dma_addr_t to_sgl_element_pair_dma(struct isci_host *ihost,
  74. struct isci_request *ireq, u32 idx)
  75. {
  76. u32 offset;
  77. if (idx == 0) {
  78. offset = (void *) &ireq->tc->sgl_pair_ab -
  79. (void *) &ihost->task_context_table[0];
  80. return ihost->task_context_dma + offset;
  81. } else if (idx == 1) {
  82. offset = (void *) &ireq->tc->sgl_pair_cd -
  83. (void *) &ihost->task_context_table[0];
  84. return ihost->task_context_dma + offset;
  85. }
  86. return sci_io_request_get_dma_addr(ireq, &ireq->sg_table[idx - 2]);
  87. }
  88. static void init_sgl_element(struct scu_sgl_element *e, struct scatterlist *sg)
  89. {
  90. e->length = sg_dma_len(sg);
  91. e->address_upper = upper_32_bits(sg_dma_address(sg));
  92. e->address_lower = lower_32_bits(sg_dma_address(sg));
  93. e->address_modifier = 0;
  94. }
  95. static void sci_request_build_sgl(struct isci_request *ireq)
  96. {
  97. struct isci_host *ihost = ireq->isci_host;
  98. struct sas_task *task = isci_request_access_task(ireq);
  99. struct scatterlist *sg = NULL;
  100. dma_addr_t dma_addr;
  101. u32 sg_idx = 0;
  102. struct scu_sgl_element_pair *scu_sg = NULL;
  103. struct scu_sgl_element_pair *prev_sg = NULL;
  104. if (task->num_scatter > 0) {
  105. sg = task->scatter;
  106. while (sg) {
  107. scu_sg = to_sgl_element_pair(ireq, sg_idx);
  108. init_sgl_element(&scu_sg->A, sg);
  109. sg = sg_next(sg);
  110. if (sg) {
  111. init_sgl_element(&scu_sg->B, sg);
  112. sg = sg_next(sg);
  113. } else
  114. memset(&scu_sg->B, 0, sizeof(scu_sg->B));
  115. if (prev_sg) {
  116. dma_addr = to_sgl_element_pair_dma(ihost,
  117. ireq,
  118. sg_idx);
  119. prev_sg->next_pair_upper =
  120. upper_32_bits(dma_addr);
  121. prev_sg->next_pair_lower =
  122. lower_32_bits(dma_addr);
  123. }
  124. prev_sg = scu_sg;
  125. sg_idx++;
  126. }
  127. } else { /* handle when no sg */
  128. scu_sg = to_sgl_element_pair(ireq, sg_idx);
  129. dma_addr = dma_map_single(&ihost->pdev->dev,
  130. task->scatter,
  131. task->total_xfer_len,
  132. task->data_dir);
  133. ireq->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 sci_io_request_build_ssp_command_iu(struct isci_request *ireq)
  144. {
  145. struct ssp_cmd_iu *cmd_iu;
  146. struct sas_task *task = isci_request_access_task(ireq);
  147. cmd_iu = &ireq->ssp.cmd;
  148. memcpy(cmd_iu->LUN, task->ssp_task.LUN, 8);
  149. cmd_iu->add_cdb_len = 0;
  150. cmd_iu->_r_a = 0;
  151. cmd_iu->_r_b = 0;
  152. cmd_iu->en_fburst = 0; /* unsupported */
  153. cmd_iu->task_prio = task->ssp_task.task_prio;
  154. cmd_iu->task_attr = task->ssp_task.task_attr;
  155. cmd_iu->_r_c = 0;
  156. sci_swab32_cpy(&cmd_iu->cdb, task->ssp_task.cdb,
  157. sizeof(task->ssp_task.cdb) / sizeof(u32));
  158. }
  159. static void sci_task_request_build_ssp_task_iu(struct isci_request *ireq)
  160. {
  161. struct ssp_task_iu *task_iu;
  162. struct sas_task *task = isci_request_access_task(ireq);
  163. struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
  164. task_iu = &ireq->ssp.tmf;
  165. memset(task_iu, 0, sizeof(struct ssp_task_iu));
  166. memcpy(task_iu->LUN, task->ssp_task.LUN, 8);
  167. task_iu->task_func = isci_tmf->tmf_code;
  168. task_iu->task_tag =
  169. (test_bit(IREQ_TMF, &ireq->flags)) ?
  170. isci_tmf->io_tag :
  171. SCI_CONTROLLER_INVALID_IO_TAG;
  172. }
  173. /**
  174. * This method is will fill in the SCU Task Context for any type of SSP request.
  175. * @sci_req:
  176. * @task_context:
  177. *
  178. */
  179. static void scu_ssp_reqeust_construct_task_context(
  180. struct isci_request *ireq,
  181. struct scu_task_context *task_context)
  182. {
  183. dma_addr_t dma_addr;
  184. struct isci_remote_device *idev;
  185. struct isci_port *iport;
  186. idev = ireq->target_device;
  187. iport = idev->owning_port;
  188. /* Fill in the TC with the its required data */
  189. task_context->abort = 0;
  190. task_context->priority = 0;
  191. task_context->initiator_request = 1;
  192. task_context->connection_rate = idev->connection_rate;
  193. task_context->protocol_engine_index = ISCI_PEG;
  194. task_context->logical_port_index = iport->physical_port_index;
  195. task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
  196. task_context->valid = SCU_TASK_CONTEXT_VALID;
  197. task_context->context_type = SCU_TASK_CONTEXT_TYPE;
  198. task_context->remote_node_index = idev->rnc.remote_node_index;
  199. task_context->command_code = 0;
  200. task_context->link_layer_control = 0;
  201. task_context->do_not_dma_ssp_good_response = 1;
  202. task_context->strict_ordering = 0;
  203. task_context->control_frame = 0;
  204. task_context->timeout_enable = 0;
  205. task_context->block_guard_enable = 0;
  206. task_context->address_modifier = 0;
  207. /* task_context->type.ssp.tag = ireq->io_tag; */
  208. task_context->task_phase = 0x01;
  209. ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  210. (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  211. (iport->physical_port_index <<
  212. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
  213. ISCI_TAG_TCI(ireq->io_tag));
  214. /*
  215. * Copy the physical address for the command buffer to the
  216. * SCU Task Context
  217. */
  218. dma_addr = sci_io_request_get_dma_addr(ireq, &ireq->ssp.cmd);
  219. task_context->command_iu_upper = upper_32_bits(dma_addr);
  220. task_context->command_iu_lower = lower_32_bits(dma_addr);
  221. /*
  222. * Copy the physical address for the response buffer to the
  223. * SCU Task Context
  224. */
  225. dma_addr = sci_io_request_get_dma_addr(ireq, &ireq->ssp.rsp);
  226. task_context->response_iu_upper = upper_32_bits(dma_addr);
  227. task_context->response_iu_lower = lower_32_bits(dma_addr);
  228. }
  229. /**
  230. * This method is will fill in the SCU Task Context for a SSP IO request.
  231. * @sci_req:
  232. *
  233. */
  234. static void scu_ssp_io_request_construct_task_context(struct isci_request *ireq,
  235. enum dma_data_direction dir,
  236. u32 len)
  237. {
  238. struct scu_task_context *task_context = ireq->tc;
  239. scu_ssp_reqeust_construct_task_context(ireq, task_context);
  240. task_context->ssp_command_iu_length =
  241. sizeof(struct ssp_cmd_iu) / sizeof(u32);
  242. task_context->type.ssp.frame_type = SSP_COMMAND;
  243. switch (dir) {
  244. case DMA_FROM_DEVICE:
  245. case DMA_NONE:
  246. default:
  247. task_context->task_type = SCU_TASK_TYPE_IOREAD;
  248. break;
  249. case DMA_TO_DEVICE:
  250. task_context->task_type = SCU_TASK_TYPE_IOWRITE;
  251. break;
  252. }
  253. task_context->transfer_length_bytes = len;
  254. if (task_context->transfer_length_bytes > 0)
  255. sci_request_build_sgl(ireq);
  256. }
  257. /**
  258. * This method will fill in the SCU Task Context for a SSP Task request. The
  259. * following important settings are utilized: -# priority ==
  260. * SCU_TASK_PRIORITY_HIGH. This ensures that the task request is issued
  261. * ahead of other task destined for the same Remote Node. -# task_type ==
  262. * SCU_TASK_TYPE_IOREAD. This simply indicates that a normal request type
  263. * (i.e. non-raw frame) is being utilized to perform task management. -#
  264. * control_frame == 1. This ensures that the proper endianess is set so
  265. * that the bytes are transmitted in the right order for a task frame.
  266. * @sci_req: This parameter specifies the task request object being
  267. * constructed.
  268. *
  269. */
  270. static void scu_ssp_task_request_construct_task_context(struct isci_request *ireq)
  271. {
  272. struct scu_task_context *task_context = ireq->tc;
  273. scu_ssp_reqeust_construct_task_context(ireq, task_context);
  274. task_context->control_frame = 1;
  275. task_context->priority = SCU_TASK_PRIORITY_HIGH;
  276. task_context->task_type = SCU_TASK_TYPE_RAW_FRAME;
  277. task_context->transfer_length_bytes = 0;
  278. task_context->type.ssp.frame_type = SSP_TASK;
  279. task_context->ssp_command_iu_length =
  280. sizeof(struct ssp_task_iu) / sizeof(u32);
  281. }
  282. /**
  283. * This method is will fill in the SCU Task Context for any type of SATA
  284. * request. This is called from the various SATA constructors.
  285. * @sci_req: The general IO request object which is to be used in
  286. * constructing the SCU task context.
  287. * @task_context: The buffer pointer for the SCU task context which is being
  288. * constructed.
  289. *
  290. * The general io request construction is complete. The buffer assignment for
  291. * the command buffer is complete. none Revisit task context construction to
  292. * determine what is common for SSP/SMP/STP task context structures.
  293. */
  294. static void scu_sata_reqeust_construct_task_context(
  295. struct isci_request *ireq,
  296. struct scu_task_context *task_context)
  297. {
  298. dma_addr_t dma_addr;
  299. struct isci_remote_device *idev;
  300. struct isci_port *iport;
  301. idev = ireq->target_device;
  302. iport = idev->owning_port;
  303. /* Fill in the TC with the its required data */
  304. task_context->abort = 0;
  305. task_context->priority = SCU_TASK_PRIORITY_NORMAL;
  306. task_context->initiator_request = 1;
  307. task_context->connection_rate = idev->connection_rate;
  308. task_context->protocol_engine_index = ISCI_PEG;
  309. task_context->logical_port_index = iport->physical_port_index;
  310. task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_STP;
  311. task_context->valid = SCU_TASK_CONTEXT_VALID;
  312. task_context->context_type = SCU_TASK_CONTEXT_TYPE;
  313. task_context->remote_node_index = idev->rnc.remote_node_index;
  314. task_context->command_code = 0;
  315. task_context->link_layer_control = 0;
  316. task_context->do_not_dma_ssp_good_response = 1;
  317. task_context->strict_ordering = 0;
  318. task_context->control_frame = 0;
  319. task_context->timeout_enable = 0;
  320. task_context->block_guard_enable = 0;
  321. task_context->address_modifier = 0;
  322. task_context->task_phase = 0x01;
  323. task_context->ssp_command_iu_length =
  324. (sizeof(struct host_to_dev_fis) - sizeof(u32)) / sizeof(u32);
  325. /* Set the first word of the H2D REG FIS */
  326. task_context->type.words[0] = *(u32 *)&ireq->stp.cmd;
  327. ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  328. (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  329. (iport->physical_port_index <<
  330. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
  331. ISCI_TAG_TCI(ireq->io_tag));
  332. /*
  333. * Copy the physical address for the command buffer to the SCU Task
  334. * Context. We must offset the command buffer by 4 bytes because the
  335. * first 4 bytes are transfered in the body of the TC.
  336. */
  337. dma_addr = sci_io_request_get_dma_addr(ireq,
  338. ((char *) &ireq->stp.cmd) +
  339. sizeof(u32));
  340. task_context->command_iu_upper = upper_32_bits(dma_addr);
  341. task_context->command_iu_lower = lower_32_bits(dma_addr);
  342. /* SATA Requests do not have a response buffer */
  343. task_context->response_iu_upper = 0;
  344. task_context->response_iu_lower = 0;
  345. }
  346. static void scu_stp_raw_request_construct_task_context(struct isci_request *ireq)
  347. {
  348. struct scu_task_context *task_context = ireq->tc;
  349. scu_sata_reqeust_construct_task_context(ireq, task_context);
  350. task_context->control_frame = 0;
  351. task_context->priority = SCU_TASK_PRIORITY_NORMAL;
  352. task_context->task_type = SCU_TASK_TYPE_SATA_RAW_FRAME;
  353. task_context->type.stp.fis_type = FIS_REGH2D;
  354. task_context->transfer_length_bytes = sizeof(struct host_to_dev_fis) - sizeof(u32);
  355. }
  356. static enum sci_status sci_stp_pio_request_construct(struct isci_request *ireq,
  357. bool copy_rx_frame)
  358. {
  359. struct isci_stp_request *stp_req = &ireq->stp.req;
  360. scu_stp_raw_request_construct_task_context(ireq);
  361. stp_req->status = 0;
  362. stp_req->sgl.offset = 0;
  363. stp_req->sgl.set = SCU_SGL_ELEMENT_PAIR_A;
  364. if (copy_rx_frame) {
  365. sci_request_build_sgl(ireq);
  366. stp_req->sgl.index = 0;
  367. } else {
  368. /* The user does not want the data copied to the SGL buffer location */
  369. stp_req->sgl.index = -1;
  370. }
  371. return SCI_SUCCESS;
  372. }
  373. /**
  374. *
  375. * @sci_req: This parameter specifies the request to be constructed as an
  376. * optimized request.
  377. * @optimized_task_type: This parameter specifies whether the request is to be
  378. * an UDMA request or a NCQ request. - A value of 0 indicates UDMA. - A
  379. * value of 1 indicates NCQ.
  380. *
  381. * This method will perform request construction common to all types of STP
  382. * requests that are optimized by the silicon (i.e. UDMA, NCQ). This method
  383. * returns an indication as to whether the construction was successful.
  384. */
  385. static void sci_stp_optimized_request_construct(struct isci_request *ireq,
  386. u8 optimized_task_type,
  387. u32 len,
  388. enum dma_data_direction dir)
  389. {
  390. struct scu_task_context *task_context = ireq->tc;
  391. /* Build the STP task context structure */
  392. scu_sata_reqeust_construct_task_context(ireq, task_context);
  393. /* Copy over the SGL elements */
  394. sci_request_build_sgl(ireq);
  395. /* Copy over the number of bytes to be transfered */
  396. task_context->transfer_length_bytes = len;
  397. if (dir == DMA_TO_DEVICE) {
  398. /*
  399. * The difference between the DMA IN and DMA OUT request task type
  400. * values are consistent with the difference between FPDMA READ
  401. * and FPDMA WRITE values. Add the supplied task type parameter
  402. * to this difference to set the task type properly for this
  403. * DATA OUT (WRITE) case. */
  404. task_context->task_type = optimized_task_type + (SCU_TASK_TYPE_DMA_OUT
  405. - SCU_TASK_TYPE_DMA_IN);
  406. } else {
  407. /*
  408. * For the DATA IN (READ) case, simply save the supplied
  409. * optimized task type. */
  410. task_context->task_type = optimized_task_type;
  411. }
  412. }
  413. static void sci_atapi_construct(struct isci_request *ireq)
  414. {
  415. struct host_to_dev_fis *h2d_fis = &ireq->stp.cmd;
  416. struct sas_task *task;
  417. /* To simplify the implementation we take advantage of the
  418. * silicon's partial acceleration of atapi protocol (dma data
  419. * transfers), so we promote all commands to dma protocol. This
  420. * breaks compatibility with ATA_HORKAGE_ATAPI_MOD16_DMA drives.
  421. */
  422. h2d_fis->features |= ATAPI_PKT_DMA;
  423. scu_stp_raw_request_construct_task_context(ireq);
  424. task = isci_request_access_task(ireq);
  425. if (task->data_dir == DMA_NONE)
  426. task->total_xfer_len = 0;
  427. /* clear the response so we can detect arrivial of an
  428. * unsolicited h2d fis
  429. */
  430. ireq->stp.rsp.fis_type = 0;
  431. }
  432. static enum sci_status
  433. sci_io_request_construct_sata(struct isci_request *ireq,
  434. u32 len,
  435. enum dma_data_direction dir,
  436. bool copy)
  437. {
  438. enum sci_status status = SCI_SUCCESS;
  439. struct sas_task *task = isci_request_access_task(ireq);
  440. struct domain_device *dev = ireq->target_device->domain_dev;
  441. /* check for management protocols */
  442. if (test_bit(IREQ_TMF, &ireq->flags)) {
  443. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  444. if (tmf->tmf_code == isci_tmf_sata_srst_high ||
  445. tmf->tmf_code == isci_tmf_sata_srst_low) {
  446. scu_stp_raw_request_construct_task_context(ireq);
  447. return SCI_SUCCESS;
  448. } else {
  449. dev_err(&ireq->owning_controller->pdev->dev,
  450. "%s: Request 0x%p received un-handled SAT "
  451. "management protocol 0x%x.\n",
  452. __func__, ireq, tmf->tmf_code);
  453. return SCI_FAILURE;
  454. }
  455. }
  456. if (!sas_protocol_ata(task->task_proto)) {
  457. dev_err(&ireq->owning_controller->pdev->dev,
  458. "%s: Non-ATA protocol in SATA path: 0x%x\n",
  459. __func__,
  460. task->task_proto);
  461. return SCI_FAILURE;
  462. }
  463. /* ATAPI */
  464. if (dev->sata_dev.command_set == ATAPI_COMMAND_SET &&
  465. task->ata_task.fis.command == ATA_CMD_PACKET) {
  466. sci_atapi_construct(ireq);
  467. return SCI_SUCCESS;
  468. }
  469. /* non data */
  470. if (task->data_dir == DMA_NONE) {
  471. scu_stp_raw_request_construct_task_context(ireq);
  472. return SCI_SUCCESS;
  473. }
  474. /* NCQ */
  475. if (task->ata_task.use_ncq) {
  476. sci_stp_optimized_request_construct(ireq,
  477. SCU_TASK_TYPE_FPDMAQ_READ,
  478. len, dir);
  479. return SCI_SUCCESS;
  480. }
  481. /* DMA */
  482. if (task->ata_task.dma_xfer) {
  483. sci_stp_optimized_request_construct(ireq,
  484. SCU_TASK_TYPE_DMA_IN,
  485. len, dir);
  486. return SCI_SUCCESS;
  487. } else /* PIO */
  488. return sci_stp_pio_request_construct(ireq, copy);
  489. return status;
  490. }
  491. static enum sci_status sci_io_request_construct_basic_ssp(struct isci_request *ireq)
  492. {
  493. struct sas_task *task = isci_request_access_task(ireq);
  494. ireq->protocol = SCIC_SSP_PROTOCOL;
  495. scu_ssp_io_request_construct_task_context(ireq,
  496. task->data_dir,
  497. task->total_xfer_len);
  498. sci_io_request_build_ssp_command_iu(ireq);
  499. sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
  500. return SCI_SUCCESS;
  501. }
  502. enum sci_status sci_task_request_construct_ssp(
  503. struct isci_request *ireq)
  504. {
  505. /* Construct the SSP Task SCU Task Context */
  506. scu_ssp_task_request_construct_task_context(ireq);
  507. /* Fill in the SSP Task IU */
  508. sci_task_request_build_ssp_task_iu(ireq);
  509. sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
  510. return SCI_SUCCESS;
  511. }
  512. static enum sci_status sci_io_request_construct_basic_sata(struct isci_request *ireq)
  513. {
  514. enum sci_status status;
  515. bool copy = false;
  516. struct sas_task *task = isci_request_access_task(ireq);
  517. ireq->protocol = SCIC_STP_PROTOCOL;
  518. copy = (task->data_dir == DMA_NONE) ? false : true;
  519. status = sci_io_request_construct_sata(ireq,
  520. task->total_xfer_len,
  521. task->data_dir,
  522. copy);
  523. if (status == SCI_SUCCESS)
  524. sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
  525. return status;
  526. }
  527. enum sci_status sci_task_request_construct_sata(struct isci_request *ireq)
  528. {
  529. enum sci_status status = SCI_SUCCESS;
  530. /* check for management protocols */
  531. if (test_bit(IREQ_TMF, &ireq->flags)) {
  532. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  533. if (tmf->tmf_code == isci_tmf_sata_srst_high ||
  534. tmf->tmf_code == isci_tmf_sata_srst_low) {
  535. scu_stp_raw_request_construct_task_context(ireq);
  536. } else {
  537. dev_err(&ireq->owning_controller->pdev->dev,
  538. "%s: Request 0x%p received un-handled SAT "
  539. "Protocol 0x%x.\n",
  540. __func__, ireq, tmf->tmf_code);
  541. return SCI_FAILURE;
  542. }
  543. }
  544. if (status != SCI_SUCCESS)
  545. return status;
  546. sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
  547. return status;
  548. }
  549. /**
  550. * sci_req_tx_bytes - bytes transferred when reply underruns request
  551. * @ireq: request that was terminated early
  552. */
  553. #define SCU_TASK_CONTEXT_SRAM 0x200000
  554. static u32 sci_req_tx_bytes(struct isci_request *ireq)
  555. {
  556. struct isci_host *ihost = ireq->owning_controller;
  557. u32 ret_val = 0;
  558. if (readl(&ihost->smu_registers->address_modifier) == 0) {
  559. void __iomem *scu_reg_base = ihost->scu_registers;
  560. /* get the bytes of data from the Address == BAR1 + 20002Ch + (256*TCi) where
  561. * BAR1 is the scu_registers
  562. * 0x20002C = 0x200000 + 0x2c
  563. * = start of task context SRAM + offset of (type.ssp.data_offset)
  564. * TCi is the io_tag of struct sci_request
  565. */
  566. ret_val = readl(scu_reg_base +
  567. (SCU_TASK_CONTEXT_SRAM + offsetof(struct scu_task_context, type.ssp.data_offset)) +
  568. ((sizeof(struct scu_task_context)) * ISCI_TAG_TCI(ireq->io_tag)));
  569. }
  570. return ret_val;
  571. }
  572. enum sci_status sci_request_start(struct isci_request *ireq)
  573. {
  574. enum sci_base_request_states state;
  575. struct scu_task_context *tc = ireq->tc;
  576. struct isci_host *ihost = ireq->owning_controller;
  577. state = ireq->sm.current_state_id;
  578. if (state != SCI_REQ_CONSTRUCTED) {
  579. dev_warn(&ihost->pdev->dev,
  580. "%s: SCIC IO Request requested to start while in wrong "
  581. "state %d\n", __func__, state);
  582. return SCI_FAILURE_INVALID_STATE;
  583. }
  584. tc->task_index = ISCI_TAG_TCI(ireq->io_tag);
  585. switch (tc->protocol_type) {
  586. case SCU_TASK_CONTEXT_PROTOCOL_SMP:
  587. case SCU_TASK_CONTEXT_PROTOCOL_SSP:
  588. /* SSP/SMP Frame */
  589. tc->type.ssp.tag = ireq->io_tag;
  590. tc->type.ssp.target_port_transfer_tag = 0xFFFF;
  591. break;
  592. case SCU_TASK_CONTEXT_PROTOCOL_STP:
  593. /* STP/SATA Frame
  594. * tc->type.stp.ncq_tag = ireq->ncq_tag;
  595. */
  596. break;
  597. case SCU_TASK_CONTEXT_PROTOCOL_NONE:
  598. /* / @todo When do we set no protocol type? */
  599. break;
  600. default:
  601. /* This should never happen since we build the IO
  602. * requests */
  603. break;
  604. }
  605. /* Add to the post_context the io tag value */
  606. ireq->post_context |= ISCI_TAG_TCI(ireq->io_tag);
  607. /* Everything is good go ahead and change state */
  608. sci_change_state(&ireq->sm, SCI_REQ_STARTED);
  609. return SCI_SUCCESS;
  610. }
  611. enum sci_status
  612. sci_io_request_terminate(struct isci_request *ireq)
  613. {
  614. enum sci_base_request_states state;
  615. state = ireq->sm.current_state_id;
  616. switch (state) {
  617. case SCI_REQ_CONSTRUCTED:
  618. ireq->scu_status = SCU_TASK_DONE_TASK_ABORT;
  619. ireq->sci_status = SCI_FAILURE_IO_TERMINATED;
  620. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  621. return SCI_SUCCESS;
  622. case SCI_REQ_STARTED:
  623. case SCI_REQ_TASK_WAIT_TC_COMP:
  624. case SCI_REQ_SMP_WAIT_RESP:
  625. case SCI_REQ_SMP_WAIT_TC_COMP:
  626. case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
  627. case SCI_REQ_STP_UDMA_WAIT_D2H:
  628. case SCI_REQ_STP_NON_DATA_WAIT_H2D:
  629. case SCI_REQ_STP_NON_DATA_WAIT_D2H:
  630. case SCI_REQ_STP_PIO_WAIT_H2D:
  631. case SCI_REQ_STP_PIO_WAIT_FRAME:
  632. case SCI_REQ_STP_PIO_DATA_IN:
  633. case SCI_REQ_STP_PIO_DATA_OUT:
  634. case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED:
  635. case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG:
  636. case SCI_REQ_STP_SOFT_RESET_WAIT_D2H:
  637. case SCI_REQ_ATAPI_WAIT_H2D:
  638. case SCI_REQ_ATAPI_WAIT_PIO_SETUP:
  639. case SCI_REQ_ATAPI_WAIT_D2H:
  640. case SCI_REQ_ATAPI_WAIT_TC_COMP:
  641. sci_change_state(&ireq->sm, SCI_REQ_ABORTING);
  642. return SCI_SUCCESS;
  643. case SCI_REQ_TASK_WAIT_TC_RESP:
  644. /* The task frame was already confirmed to have been
  645. * sent by the SCU HW. Since the state machine is
  646. * now only waiting for the task response itself,
  647. * abort the request and complete it immediately
  648. * and don't wait for the task response.
  649. */
  650. sci_change_state(&ireq->sm, SCI_REQ_ABORTING);
  651. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  652. return SCI_SUCCESS;
  653. case SCI_REQ_ABORTING:
  654. /* If a request has a termination requested twice, return
  655. * a failure indication, since HW confirmation of the first
  656. * abort is still outstanding.
  657. */
  658. case SCI_REQ_COMPLETED:
  659. default:
  660. dev_warn(&ireq->owning_controller->pdev->dev,
  661. "%s: SCIC IO Request requested to abort while in wrong "
  662. "state %d\n",
  663. __func__,
  664. ireq->sm.current_state_id);
  665. break;
  666. }
  667. return SCI_FAILURE_INVALID_STATE;
  668. }
  669. enum sci_status sci_request_complete(struct isci_request *ireq)
  670. {
  671. enum sci_base_request_states state;
  672. struct isci_host *ihost = ireq->owning_controller;
  673. state = ireq->sm.current_state_id;
  674. if (WARN_ONCE(state != SCI_REQ_COMPLETED,
  675. "isci: request completion from wrong state (%d)\n", state))
  676. return SCI_FAILURE_INVALID_STATE;
  677. if (ireq->saved_rx_frame_index != SCU_INVALID_FRAME_INDEX)
  678. sci_controller_release_frame(ihost,
  679. ireq->saved_rx_frame_index);
  680. /* XXX can we just stop the machine and remove the 'final' state? */
  681. sci_change_state(&ireq->sm, SCI_REQ_FINAL);
  682. return SCI_SUCCESS;
  683. }
  684. enum sci_status sci_io_request_event_handler(struct isci_request *ireq,
  685. u32 event_code)
  686. {
  687. enum sci_base_request_states state;
  688. struct isci_host *ihost = ireq->owning_controller;
  689. state = ireq->sm.current_state_id;
  690. if (state != SCI_REQ_STP_PIO_DATA_IN) {
  691. dev_warn(&ihost->pdev->dev, "%s: (%x) in wrong state %d\n",
  692. __func__, event_code, state);
  693. return SCI_FAILURE_INVALID_STATE;
  694. }
  695. switch (scu_get_event_specifier(event_code)) {
  696. case SCU_TASK_DONE_CRC_ERR << SCU_EVENT_SPECIFIC_CODE_SHIFT:
  697. /* We are waiting for data and the SCU has R_ERR the data frame.
  698. * Go back to waiting for the D2H Register FIS
  699. */
  700. sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
  701. return SCI_SUCCESS;
  702. default:
  703. dev_err(&ihost->pdev->dev,
  704. "%s: pio request unexpected event %#x\n",
  705. __func__, event_code);
  706. /* TODO Should we fail the PIO request when we get an
  707. * unexpected event?
  708. */
  709. return SCI_FAILURE;
  710. }
  711. }
  712. /*
  713. * This function copies response data for requests returning response data
  714. * instead of sense data.
  715. * @sci_req: This parameter specifies the request object for which to copy
  716. * the response data.
  717. */
  718. static void sci_io_request_copy_response(struct isci_request *ireq)
  719. {
  720. void *resp_buf;
  721. u32 len;
  722. struct ssp_response_iu *ssp_response;
  723. struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
  724. ssp_response = &ireq->ssp.rsp;
  725. resp_buf = &isci_tmf->resp.resp_iu;
  726. len = min_t(u32,
  727. SSP_RESP_IU_MAX_SIZE,
  728. be32_to_cpu(ssp_response->response_data_len));
  729. memcpy(resp_buf, ssp_response->resp_data, len);
  730. }
  731. static enum sci_status
  732. request_started_state_tc_event(struct isci_request *ireq,
  733. u32 completion_code)
  734. {
  735. struct ssp_response_iu *resp_iu;
  736. u8 datapres;
  737. /* TODO: Any SDMA return code of other than 0 is bad decode 0x003C0000
  738. * to determine SDMA status
  739. */
  740. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  741. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  742. ireq->scu_status = SCU_TASK_DONE_GOOD;
  743. ireq->sci_status = SCI_SUCCESS;
  744. break;
  745. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EARLY_RESP): {
  746. /* There are times when the SCU hardware will return an early
  747. * response because the io request specified more data than is
  748. * returned by the target device (mode pages, inquiry data,
  749. * etc.). We must check the response stats to see if this is
  750. * truly a failed request or a good request that just got
  751. * completed early.
  752. */
  753. struct ssp_response_iu *resp = &ireq->ssp.rsp;
  754. ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
  755. sci_swab32_cpy(&ireq->ssp.rsp,
  756. &ireq->ssp.rsp,
  757. word_cnt);
  758. if (resp->status == 0) {
  759. ireq->scu_status = SCU_TASK_DONE_GOOD;
  760. ireq->sci_status = SCI_SUCCESS_IO_DONE_EARLY;
  761. } else {
  762. ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
  763. ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
  764. }
  765. break;
  766. }
  767. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CHECK_RESPONSE): {
  768. ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
  769. sci_swab32_cpy(&ireq->ssp.rsp,
  770. &ireq->ssp.rsp,
  771. word_cnt);
  772. ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
  773. ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
  774. break;
  775. }
  776. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RESP_LEN_ERR):
  777. /* TODO With TASK_DONE_RESP_LEN_ERR is the response frame
  778. * guaranteed to be received before this completion status is
  779. * posted?
  780. */
  781. resp_iu = &ireq->ssp.rsp;
  782. datapres = resp_iu->datapres;
  783. if (datapres == 1 || datapres == 2) {
  784. ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
  785. ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
  786. } else {
  787. ireq->scu_status = SCU_TASK_DONE_GOOD;
  788. ireq->sci_status = SCI_SUCCESS;
  789. }
  790. break;
  791. /* only stp device gets suspended. */
  792. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
  793. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_PERR):
  794. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_ERR):
  795. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_DATA_LEN_ERR):
  796. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_ABORT_ERR):
  797. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_WD_LEN):
  798. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
  799. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_RESP):
  800. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_SDBFIS):
  801. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
  802. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDB_ERR):
  803. if (ireq->protocol == SCIC_STP_PROTOCOL) {
  804. ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  805. SCU_COMPLETION_TL_STATUS_SHIFT;
  806. ireq->sci_status = SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
  807. } else {
  808. ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  809. SCU_COMPLETION_TL_STATUS_SHIFT;
  810. ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
  811. }
  812. break;
  813. /* both stp/ssp device gets suspended */
  814. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LF_ERR):
  815. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_WRONG_DESTINATION):
  816. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1):
  817. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2):
  818. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3):
  819. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_BAD_DESTINATION):
  820. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_ZONE_VIOLATION):
  821. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY):
  822. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED):
  823. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED):
  824. ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  825. SCU_COMPLETION_TL_STATUS_SHIFT;
  826. ireq->sci_status = SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
  827. break;
  828. /* neither ssp nor stp gets suspended. */
  829. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_CMD_ERR):
  830. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_XR):
  831. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_IU_LEN_ERR):
  832. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDMA_ERR):
  833. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OFFSET_ERR):
  834. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EXCESS_DATA):
  835. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
  836. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
  837. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
  838. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
  839. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_DATA):
  840. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OPEN_FAIL):
  841. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_VIIT_ENTRY_NV):
  842. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_IIT_ENTRY_NV):
  843. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RNCNV_OUTBOUND):
  844. default:
  845. ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  846. SCU_COMPLETION_TL_STATUS_SHIFT;
  847. ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
  848. break;
  849. }
  850. /*
  851. * TODO: This is probably wrong for ACK/NAK timeout conditions
  852. */
  853. /* In all cases we will treat this as the completion of the IO req. */
  854. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  855. return SCI_SUCCESS;
  856. }
  857. static enum sci_status
  858. request_aborting_state_tc_event(struct isci_request *ireq,
  859. u32 completion_code)
  860. {
  861. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  862. case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
  863. case (SCU_TASK_DONE_TASK_ABORT << SCU_COMPLETION_TL_STATUS_SHIFT):
  864. ireq->scu_status = SCU_TASK_DONE_TASK_ABORT;
  865. ireq->sci_status = SCI_FAILURE_IO_TERMINATED;
  866. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  867. break;
  868. default:
  869. /* Unless we get some strange error wait for the task abort to complete
  870. * TODO: Should there be a state change for this completion?
  871. */
  872. break;
  873. }
  874. return SCI_SUCCESS;
  875. }
  876. static enum sci_status ssp_task_request_await_tc_event(struct isci_request *ireq,
  877. u32 completion_code)
  878. {
  879. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  880. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  881. ireq->scu_status = SCU_TASK_DONE_GOOD;
  882. ireq->sci_status = SCI_SUCCESS;
  883. sci_change_state(&ireq->sm, SCI_REQ_TASK_WAIT_TC_RESP);
  884. break;
  885. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
  886. /* Currently, the decision is to simply allow the task request
  887. * to timeout if the task IU wasn't received successfully.
  888. * There is a potential for receiving multiple task responses if
  889. * we decide to send the task IU again.
  890. */
  891. dev_warn(&ireq->owning_controller->pdev->dev,
  892. "%s: TaskRequest:0x%p CompletionCode:%x - "
  893. "ACK/NAK timeout\n", __func__, ireq,
  894. completion_code);
  895. sci_change_state(&ireq->sm, SCI_REQ_TASK_WAIT_TC_RESP);
  896. break;
  897. default:
  898. /*
  899. * All other completion status cause the IO to be complete.
  900. * If a NAK was received, then it is up to the user to retry
  901. * the request.
  902. */
  903. ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
  904. ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
  905. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  906. break;
  907. }
  908. return SCI_SUCCESS;
  909. }
  910. static enum sci_status
  911. smp_request_await_response_tc_event(struct isci_request *ireq,
  912. u32 completion_code)
  913. {
  914. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  915. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  916. /* In the AWAIT RESPONSE state, any TC completion is
  917. * unexpected. but if the TC has success status, we
  918. * complete the IO anyway.
  919. */
  920. ireq->scu_status = SCU_TASK_DONE_GOOD;
  921. ireq->sci_status = SCI_SUCCESS;
  922. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  923. break;
  924. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
  925. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
  926. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
  927. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
  928. /* These status has been seen in a specific LSI
  929. * expander, which sometimes is not able to send smp
  930. * response within 2 ms. This causes our hardware break
  931. * the connection and set TC completion with one of
  932. * these SMP_XXX_XX_ERR status. For these type of error,
  933. * we ask ihost user to retry the request.
  934. */
  935. ireq->scu_status = SCU_TASK_DONE_SMP_RESP_TO_ERR;
  936. ireq->sci_status = SCI_FAILURE_RETRY_REQUIRED;
  937. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  938. break;
  939. default:
  940. /* All other completion status cause the IO to be complete. If a NAK
  941. * was received, then it is up to the user to retry the request
  942. */
  943. ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
  944. ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
  945. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  946. break;
  947. }
  948. return SCI_SUCCESS;
  949. }
  950. static enum sci_status
  951. smp_request_await_tc_event(struct isci_request *ireq,
  952. u32 completion_code)
  953. {
  954. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  955. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  956. ireq->scu_status = SCU_TASK_DONE_GOOD;
  957. ireq->sci_status = SCI_SUCCESS;
  958. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  959. break;
  960. default:
  961. /* All other completion status cause the IO to be
  962. * complete. If a NAK was received, then it is up to
  963. * the user to retry the request.
  964. */
  965. ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
  966. ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
  967. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  968. break;
  969. }
  970. return SCI_SUCCESS;
  971. }
  972. static struct scu_sgl_element *pio_sgl_next(struct isci_stp_request *stp_req)
  973. {
  974. struct scu_sgl_element *sgl;
  975. struct scu_sgl_element_pair *sgl_pair;
  976. struct isci_request *ireq = to_ireq(stp_req);
  977. struct isci_stp_pio_sgl *pio_sgl = &stp_req->sgl;
  978. sgl_pair = to_sgl_element_pair(ireq, pio_sgl->index);
  979. if (!sgl_pair)
  980. sgl = NULL;
  981. else if (pio_sgl->set == SCU_SGL_ELEMENT_PAIR_A) {
  982. if (sgl_pair->B.address_lower == 0 &&
  983. sgl_pair->B.address_upper == 0) {
  984. sgl = NULL;
  985. } else {
  986. pio_sgl->set = SCU_SGL_ELEMENT_PAIR_B;
  987. sgl = &sgl_pair->B;
  988. }
  989. } else {
  990. if (sgl_pair->next_pair_lower == 0 &&
  991. sgl_pair->next_pair_upper == 0) {
  992. sgl = NULL;
  993. } else {
  994. pio_sgl->index++;
  995. pio_sgl->set = SCU_SGL_ELEMENT_PAIR_A;
  996. sgl_pair = to_sgl_element_pair(ireq, pio_sgl->index);
  997. sgl = &sgl_pair->A;
  998. }
  999. }
  1000. return sgl;
  1001. }
  1002. static enum sci_status
  1003. stp_request_non_data_await_h2d_tc_event(struct isci_request *ireq,
  1004. u32 completion_code)
  1005. {
  1006. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1007. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1008. ireq->scu_status = SCU_TASK_DONE_GOOD;
  1009. ireq->sci_status = SCI_SUCCESS;
  1010. sci_change_state(&ireq->sm, SCI_REQ_STP_NON_DATA_WAIT_D2H);
  1011. break;
  1012. default:
  1013. /* All other completion status cause the IO to be
  1014. * complete. If a NAK was received, then it is up to
  1015. * the user to retry the request.
  1016. */
  1017. ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
  1018. ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
  1019. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1020. break;
  1021. }
  1022. return SCI_SUCCESS;
  1023. }
  1024. #define SCU_MAX_FRAME_BUFFER_SIZE 0x400 /* 1K is the maximum SCU frame data payload */
  1025. /* transmit DATA_FIS from (current sgl + offset) for input
  1026. * parameter length. current sgl and offset is alreay stored in the IO request
  1027. */
  1028. static enum sci_status sci_stp_request_pio_data_out_trasmit_data_frame(
  1029. struct isci_request *ireq,
  1030. u32 length)
  1031. {
  1032. struct isci_stp_request *stp_req = &ireq->stp.req;
  1033. struct scu_task_context *task_context = ireq->tc;
  1034. struct scu_sgl_element_pair *sgl_pair;
  1035. struct scu_sgl_element *current_sgl;
  1036. /* Recycle the TC and reconstruct it for sending out DATA FIS containing
  1037. * for the data from current_sgl+offset for the input length
  1038. */
  1039. sgl_pair = to_sgl_element_pair(ireq, stp_req->sgl.index);
  1040. if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A)
  1041. current_sgl = &sgl_pair->A;
  1042. else
  1043. current_sgl = &sgl_pair->B;
  1044. /* update the TC */
  1045. task_context->command_iu_upper = current_sgl->address_upper;
  1046. task_context->command_iu_lower = current_sgl->address_lower;
  1047. task_context->transfer_length_bytes = length;
  1048. task_context->type.stp.fis_type = FIS_DATA;
  1049. /* send the new TC out. */
  1050. return sci_controller_continue_io(ireq);
  1051. }
  1052. static enum sci_status sci_stp_request_pio_data_out_transmit_data(struct isci_request *ireq)
  1053. {
  1054. struct isci_stp_request *stp_req = &ireq->stp.req;
  1055. struct scu_sgl_element_pair *sgl_pair;
  1056. enum sci_status status = SCI_SUCCESS;
  1057. struct scu_sgl_element *sgl;
  1058. u32 offset;
  1059. u32 len = 0;
  1060. offset = stp_req->sgl.offset;
  1061. sgl_pair = to_sgl_element_pair(ireq, stp_req->sgl.index);
  1062. if (WARN_ONCE(!sgl_pair, "%s: null sgl element", __func__))
  1063. return SCI_FAILURE;
  1064. if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A) {
  1065. sgl = &sgl_pair->A;
  1066. len = sgl_pair->A.length - offset;
  1067. } else {
  1068. sgl = &sgl_pair->B;
  1069. len = sgl_pair->B.length - offset;
  1070. }
  1071. if (stp_req->pio_len == 0)
  1072. return SCI_SUCCESS;
  1073. if (stp_req->pio_len >= len) {
  1074. status = sci_stp_request_pio_data_out_trasmit_data_frame(ireq, len);
  1075. if (status != SCI_SUCCESS)
  1076. return status;
  1077. stp_req->pio_len -= len;
  1078. /* update the current sgl, offset and save for future */
  1079. sgl = pio_sgl_next(stp_req);
  1080. offset = 0;
  1081. } else if (stp_req->pio_len < len) {
  1082. sci_stp_request_pio_data_out_trasmit_data_frame(ireq, stp_req->pio_len);
  1083. /* Sgl offset will be adjusted and saved for future */
  1084. offset += stp_req->pio_len;
  1085. sgl->address_lower += stp_req->pio_len;
  1086. stp_req->pio_len = 0;
  1087. }
  1088. stp_req->sgl.offset = offset;
  1089. return status;
  1090. }
  1091. /**
  1092. *
  1093. * @stp_request: The request that is used for the SGL processing.
  1094. * @data_buffer: The buffer of data to be copied.
  1095. * @length: The length of the data transfer.
  1096. *
  1097. * Copy the data from the buffer for the length specified to the IO reqeust SGL
  1098. * specified data region. enum sci_status
  1099. */
  1100. static enum sci_status
  1101. sci_stp_request_pio_data_in_copy_data_buffer(struct isci_stp_request *stp_req,
  1102. u8 *data_buf, u32 len)
  1103. {
  1104. struct isci_request *ireq;
  1105. u8 *src_addr;
  1106. int copy_len;
  1107. struct sas_task *task;
  1108. struct scatterlist *sg;
  1109. void *kaddr;
  1110. int total_len = len;
  1111. ireq = to_ireq(stp_req);
  1112. task = isci_request_access_task(ireq);
  1113. src_addr = data_buf;
  1114. if (task->num_scatter > 0) {
  1115. sg = task->scatter;
  1116. while (total_len > 0) {
  1117. struct page *page = sg_page(sg);
  1118. copy_len = min_t(int, total_len, sg_dma_len(sg));
  1119. kaddr = kmap_atomic(page, KM_IRQ0);
  1120. memcpy(kaddr + sg->offset, src_addr, copy_len);
  1121. kunmap_atomic(kaddr, KM_IRQ0);
  1122. total_len -= copy_len;
  1123. src_addr += copy_len;
  1124. sg = sg_next(sg);
  1125. }
  1126. } else {
  1127. BUG_ON(task->total_xfer_len < total_len);
  1128. memcpy(task->scatter, src_addr, total_len);
  1129. }
  1130. return SCI_SUCCESS;
  1131. }
  1132. /**
  1133. *
  1134. * @sci_req: The PIO DATA IN request that is to receive the data.
  1135. * @data_buffer: The buffer to copy from.
  1136. *
  1137. * Copy the data buffer to the io request data region. enum sci_status
  1138. */
  1139. static enum sci_status sci_stp_request_pio_data_in_copy_data(
  1140. struct isci_stp_request *stp_req,
  1141. u8 *data_buffer)
  1142. {
  1143. enum sci_status status;
  1144. /*
  1145. * If there is less than 1K remaining in the transfer request
  1146. * copy just the data for the transfer */
  1147. if (stp_req->pio_len < SCU_MAX_FRAME_BUFFER_SIZE) {
  1148. status = sci_stp_request_pio_data_in_copy_data_buffer(
  1149. stp_req, data_buffer, stp_req->pio_len);
  1150. if (status == SCI_SUCCESS)
  1151. stp_req->pio_len = 0;
  1152. } else {
  1153. /* We are transfering the whole frame so copy */
  1154. status = sci_stp_request_pio_data_in_copy_data_buffer(
  1155. stp_req, data_buffer, SCU_MAX_FRAME_BUFFER_SIZE);
  1156. if (status == SCI_SUCCESS)
  1157. stp_req->pio_len -= SCU_MAX_FRAME_BUFFER_SIZE;
  1158. }
  1159. return status;
  1160. }
  1161. static enum sci_status
  1162. stp_request_pio_await_h2d_completion_tc_event(struct isci_request *ireq,
  1163. u32 completion_code)
  1164. {
  1165. enum sci_status status = SCI_SUCCESS;
  1166. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1167. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1168. ireq->scu_status = SCU_TASK_DONE_GOOD;
  1169. ireq->sci_status = SCI_SUCCESS;
  1170. sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
  1171. break;
  1172. default:
  1173. /* All other completion status cause the IO to be
  1174. * complete. If a NAK was received, then it is up to
  1175. * the user to retry the request.
  1176. */
  1177. ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
  1178. ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
  1179. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1180. break;
  1181. }
  1182. return status;
  1183. }
  1184. static enum sci_status
  1185. pio_data_out_tx_done_tc_event(struct isci_request *ireq,
  1186. u32 completion_code)
  1187. {
  1188. enum sci_status status = SCI_SUCCESS;
  1189. bool all_frames_transferred = false;
  1190. struct isci_stp_request *stp_req = &ireq->stp.req;
  1191. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1192. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1193. /* Transmit data */
  1194. if (stp_req->pio_len != 0) {
  1195. status = sci_stp_request_pio_data_out_transmit_data(ireq);
  1196. if (status == SCI_SUCCESS) {
  1197. if (stp_req->pio_len == 0)
  1198. all_frames_transferred = true;
  1199. }
  1200. } else if (stp_req->pio_len == 0) {
  1201. /*
  1202. * this will happen if the all data is written at the
  1203. * first time after the pio setup fis is received
  1204. */
  1205. all_frames_transferred = true;
  1206. }
  1207. /* all data transferred. */
  1208. if (all_frames_transferred) {
  1209. /*
  1210. * Change the state to SCI_REQ_STP_PIO_DATA_IN
  1211. * and wait for PIO_SETUP fis / or D2H REg fis. */
  1212. sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
  1213. }
  1214. break;
  1215. default:
  1216. /*
  1217. * All other completion status cause the IO to be complete.
  1218. * If a NAK was received, then it is up to the user to retry
  1219. * the request.
  1220. */
  1221. ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
  1222. ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
  1223. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1224. break;
  1225. }
  1226. return status;
  1227. }
  1228. static enum sci_status sci_stp_request_udma_general_frame_handler(struct isci_request *ireq,
  1229. u32 frame_index)
  1230. {
  1231. struct isci_host *ihost = ireq->owning_controller;
  1232. struct dev_to_host_fis *frame_header;
  1233. enum sci_status status;
  1234. u32 *frame_buffer;
  1235. status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
  1236. frame_index,
  1237. (void **)&frame_header);
  1238. if ((status == SCI_SUCCESS) &&
  1239. (frame_header->fis_type == FIS_REGD2H)) {
  1240. sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
  1241. frame_index,
  1242. (void **)&frame_buffer);
  1243. sci_controller_copy_sata_response(&ireq->stp.rsp,
  1244. frame_header,
  1245. frame_buffer);
  1246. }
  1247. sci_controller_release_frame(ihost, frame_index);
  1248. return status;
  1249. }
  1250. static enum sci_status process_unsolicited_fis(struct isci_request *ireq,
  1251. u32 frame_index)
  1252. {
  1253. struct isci_host *ihost = ireq->owning_controller;
  1254. enum sci_status status;
  1255. struct dev_to_host_fis *frame_header;
  1256. u32 *frame_buffer;
  1257. status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
  1258. frame_index,
  1259. (void **)&frame_header);
  1260. if (status != SCI_SUCCESS)
  1261. return status;
  1262. if (frame_header->fis_type != FIS_REGD2H) {
  1263. dev_err(&ireq->isci_host->pdev->dev,
  1264. "%s ERROR: invalid fis type 0x%X\n",
  1265. __func__, frame_header->fis_type);
  1266. return SCI_FAILURE;
  1267. }
  1268. sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
  1269. frame_index,
  1270. (void **)&frame_buffer);
  1271. sci_controller_copy_sata_response(&ireq->stp.rsp,
  1272. (u32 *)frame_header,
  1273. frame_buffer);
  1274. /* Frame has been decoded return it to the controller */
  1275. sci_controller_release_frame(ihost, frame_index);
  1276. return status;
  1277. }
  1278. static enum sci_status atapi_d2h_reg_frame_handler(struct isci_request *ireq,
  1279. u32 frame_index)
  1280. {
  1281. struct sas_task *task = isci_request_access_task(ireq);
  1282. enum sci_status status;
  1283. status = process_unsolicited_fis(ireq, frame_index);
  1284. if (status == SCI_SUCCESS) {
  1285. if (ireq->stp.rsp.status & ATA_ERR)
  1286. status = SCI_IO_FAILURE_RESPONSE_VALID;
  1287. } else {
  1288. status = SCI_IO_FAILURE_RESPONSE_VALID;
  1289. }
  1290. if (status != SCI_SUCCESS) {
  1291. ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
  1292. ireq->sci_status = status;
  1293. } else {
  1294. ireq->scu_status = SCU_TASK_DONE_GOOD;
  1295. ireq->sci_status = SCI_SUCCESS;
  1296. }
  1297. /* the d2h ufi is the end of non-data commands */
  1298. if (task->data_dir == DMA_NONE)
  1299. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1300. return status;
  1301. }
  1302. static void scu_atapi_reconstruct_raw_frame_task_context(struct isci_request *ireq)
  1303. {
  1304. struct ata_device *dev = sas_to_ata_dev(ireq->target_device->domain_dev);
  1305. void *atapi_cdb = ireq->ttype_ptr.io_task_ptr->ata_task.atapi_packet;
  1306. struct scu_task_context *task_context = ireq->tc;
  1307. /* fill in the SCU Task Context for a DATA fis containing CDB in Raw Frame
  1308. * type. The TC for previous Packet fis was already there, we only need to
  1309. * change the H2D fis content.
  1310. */
  1311. memset(&ireq->stp.cmd, 0, sizeof(struct host_to_dev_fis));
  1312. memcpy(((u8 *)&ireq->stp.cmd + sizeof(u32)), atapi_cdb, ATAPI_CDB_LEN);
  1313. memset(&(task_context->type.stp), 0, sizeof(struct stp_task_context));
  1314. task_context->type.stp.fis_type = FIS_DATA;
  1315. task_context->transfer_length_bytes = dev->cdb_len;
  1316. }
  1317. static void scu_atapi_construct_task_context(struct isci_request *ireq)
  1318. {
  1319. struct ata_device *dev = sas_to_ata_dev(ireq->target_device->domain_dev);
  1320. struct sas_task *task = isci_request_access_task(ireq);
  1321. struct scu_task_context *task_context = ireq->tc;
  1322. int cdb_len = dev->cdb_len;
  1323. /* reference: SSTL 1.13.4.2
  1324. * task_type, sata_direction
  1325. */
  1326. if (task->data_dir == DMA_TO_DEVICE) {
  1327. task_context->task_type = SCU_TASK_TYPE_PACKET_DMA_OUT;
  1328. task_context->sata_direction = 0;
  1329. } else {
  1330. /* todo: for NO_DATA command, we need to send out raw frame. */
  1331. task_context->task_type = SCU_TASK_TYPE_PACKET_DMA_IN;
  1332. task_context->sata_direction = 1;
  1333. }
  1334. memset(&task_context->type.stp, 0, sizeof(task_context->type.stp));
  1335. task_context->type.stp.fis_type = FIS_DATA;
  1336. memset(&ireq->stp.cmd, 0, sizeof(ireq->stp.cmd));
  1337. memcpy(&ireq->stp.cmd.lbal, task->ata_task.atapi_packet, cdb_len);
  1338. task_context->ssp_command_iu_length = cdb_len / sizeof(u32);
  1339. /* task phase is set to TX_CMD */
  1340. task_context->task_phase = 0x1;
  1341. /* retry counter */
  1342. task_context->stp_retry_count = 0;
  1343. /* data transfer size. */
  1344. task_context->transfer_length_bytes = task->total_xfer_len;
  1345. /* setup sgl */
  1346. sci_request_build_sgl(ireq);
  1347. }
  1348. enum sci_status
  1349. sci_io_request_frame_handler(struct isci_request *ireq,
  1350. u32 frame_index)
  1351. {
  1352. struct isci_host *ihost = ireq->owning_controller;
  1353. struct isci_stp_request *stp_req = &ireq->stp.req;
  1354. enum sci_base_request_states state;
  1355. enum sci_status status;
  1356. ssize_t word_cnt;
  1357. state = ireq->sm.current_state_id;
  1358. switch (state) {
  1359. case SCI_REQ_STARTED: {
  1360. struct ssp_frame_hdr ssp_hdr;
  1361. void *frame_header;
  1362. sci_unsolicited_frame_control_get_header(&ihost->uf_control,
  1363. frame_index,
  1364. &frame_header);
  1365. word_cnt = sizeof(struct ssp_frame_hdr) / sizeof(u32);
  1366. sci_swab32_cpy(&ssp_hdr, frame_header, word_cnt);
  1367. if (ssp_hdr.frame_type == SSP_RESPONSE) {
  1368. struct ssp_response_iu *resp_iu;
  1369. ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
  1370. sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
  1371. frame_index,
  1372. (void **)&resp_iu);
  1373. sci_swab32_cpy(&ireq->ssp.rsp, resp_iu, word_cnt);
  1374. resp_iu = &ireq->ssp.rsp;
  1375. if (resp_iu->datapres == 0x01 ||
  1376. resp_iu->datapres == 0x02) {
  1377. ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
  1378. ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
  1379. } else {
  1380. ireq->scu_status = SCU_TASK_DONE_GOOD;
  1381. ireq->sci_status = SCI_SUCCESS;
  1382. }
  1383. } else {
  1384. /* not a response frame, why did it get forwarded? */
  1385. dev_err(&ihost->pdev->dev,
  1386. "%s: SCIC IO Request 0x%p received unexpected "
  1387. "frame %d type 0x%02x\n", __func__, ireq,
  1388. frame_index, ssp_hdr.frame_type);
  1389. }
  1390. /*
  1391. * In any case we are done with this frame buffer return it to
  1392. * the controller
  1393. */
  1394. sci_controller_release_frame(ihost, frame_index);
  1395. return SCI_SUCCESS;
  1396. }
  1397. case SCI_REQ_TASK_WAIT_TC_RESP:
  1398. sci_io_request_copy_response(ireq);
  1399. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1400. sci_controller_release_frame(ihost, frame_index);
  1401. return SCI_SUCCESS;
  1402. case SCI_REQ_SMP_WAIT_RESP: {
  1403. struct sas_task *task = isci_request_access_task(ireq);
  1404. struct scatterlist *sg = &task->smp_task.smp_resp;
  1405. void *frame_header, *kaddr;
  1406. u8 *rsp;
  1407. sci_unsolicited_frame_control_get_header(&ihost->uf_control,
  1408. frame_index,
  1409. &frame_header);
  1410. kaddr = kmap_atomic(sg_page(sg), KM_IRQ0);
  1411. rsp = kaddr + sg->offset;
  1412. sci_swab32_cpy(rsp, frame_header, 1);
  1413. if (rsp[0] == SMP_RESPONSE) {
  1414. void *smp_resp;
  1415. sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
  1416. frame_index,
  1417. &smp_resp);
  1418. word_cnt = (sg->length/4)-1;
  1419. if (word_cnt > 0)
  1420. word_cnt = min_t(unsigned int, word_cnt,
  1421. SCU_UNSOLICITED_FRAME_BUFFER_SIZE/4);
  1422. sci_swab32_cpy(rsp + 4, smp_resp, word_cnt);
  1423. ireq->scu_status = SCU_TASK_DONE_GOOD;
  1424. ireq->sci_status = SCI_SUCCESS;
  1425. sci_change_state(&ireq->sm, SCI_REQ_SMP_WAIT_TC_COMP);
  1426. } else {
  1427. /*
  1428. * This was not a response frame why did it get
  1429. * forwarded?
  1430. */
  1431. dev_err(&ihost->pdev->dev,
  1432. "%s: SCIC SMP Request 0x%p received unexpected "
  1433. "frame %d type 0x%02x\n",
  1434. __func__,
  1435. ireq,
  1436. frame_index,
  1437. rsp[0]);
  1438. ireq->scu_status = SCU_TASK_DONE_SMP_FRM_TYPE_ERR;
  1439. ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
  1440. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1441. }
  1442. kunmap_atomic(kaddr, KM_IRQ0);
  1443. sci_controller_release_frame(ihost, frame_index);
  1444. return SCI_SUCCESS;
  1445. }
  1446. case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
  1447. return sci_stp_request_udma_general_frame_handler(ireq,
  1448. frame_index);
  1449. case SCI_REQ_STP_UDMA_WAIT_D2H:
  1450. /* Use the general frame handler to copy the resposne data */
  1451. status = sci_stp_request_udma_general_frame_handler(ireq, frame_index);
  1452. if (status != SCI_SUCCESS)
  1453. return status;
  1454. ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
  1455. ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
  1456. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1457. return SCI_SUCCESS;
  1458. case SCI_REQ_STP_NON_DATA_WAIT_D2H: {
  1459. struct dev_to_host_fis *frame_header;
  1460. u32 *frame_buffer;
  1461. status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
  1462. frame_index,
  1463. (void **)&frame_header);
  1464. if (status != SCI_SUCCESS) {
  1465. dev_err(&ihost->pdev->dev,
  1466. "%s: SCIC IO Request 0x%p could not get frame "
  1467. "header for frame index %d, status %x\n",
  1468. __func__,
  1469. stp_req,
  1470. frame_index,
  1471. status);
  1472. return status;
  1473. }
  1474. switch (frame_header->fis_type) {
  1475. case FIS_REGD2H:
  1476. sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
  1477. frame_index,
  1478. (void **)&frame_buffer);
  1479. sci_controller_copy_sata_response(&ireq->stp.rsp,
  1480. frame_header,
  1481. frame_buffer);
  1482. /* The command has completed with error */
  1483. ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
  1484. ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
  1485. break;
  1486. default:
  1487. dev_warn(&ihost->pdev->dev,
  1488. "%s: IO Request:0x%p Frame Id:%d protocol "
  1489. "violation occurred\n", __func__, stp_req,
  1490. frame_index);
  1491. ireq->scu_status = SCU_TASK_DONE_UNEXP_FIS;
  1492. ireq->sci_status = SCI_FAILURE_PROTOCOL_VIOLATION;
  1493. break;
  1494. }
  1495. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1496. /* Frame has been decoded return it to the controller */
  1497. sci_controller_release_frame(ihost, frame_index);
  1498. return status;
  1499. }
  1500. case SCI_REQ_STP_PIO_WAIT_FRAME: {
  1501. struct sas_task *task = isci_request_access_task(ireq);
  1502. struct dev_to_host_fis *frame_header;
  1503. u32 *frame_buffer;
  1504. status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
  1505. frame_index,
  1506. (void **)&frame_header);
  1507. if (status != SCI_SUCCESS) {
  1508. dev_err(&ihost->pdev->dev,
  1509. "%s: SCIC IO Request 0x%p could not get frame "
  1510. "header for frame index %d, status %x\n",
  1511. __func__, stp_req, frame_index, status);
  1512. return status;
  1513. }
  1514. switch (frame_header->fis_type) {
  1515. case FIS_PIO_SETUP:
  1516. /* Get from the frame buffer the PIO Setup Data */
  1517. sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
  1518. frame_index,
  1519. (void **)&frame_buffer);
  1520. /* Get the data from the PIO Setup The SCU Hardware
  1521. * returns first word in the frame_header and the rest
  1522. * of the data is in the frame buffer so we need to
  1523. * back up one dword
  1524. */
  1525. /* transfer_count: first 16bits in the 4th dword */
  1526. stp_req->pio_len = frame_buffer[3] & 0xffff;
  1527. /* status: 4th byte in the 3rd dword */
  1528. stp_req->status = (frame_buffer[2] >> 24) & 0xff;
  1529. sci_controller_copy_sata_response(&ireq->stp.rsp,
  1530. frame_header,
  1531. frame_buffer);
  1532. ireq->stp.rsp.status = stp_req->status;
  1533. /* The next state is dependent on whether the
  1534. * request was PIO Data-in or Data out
  1535. */
  1536. if (task->data_dir == DMA_FROM_DEVICE) {
  1537. sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_DATA_IN);
  1538. } else if (task->data_dir == DMA_TO_DEVICE) {
  1539. /* Transmit data */
  1540. status = sci_stp_request_pio_data_out_transmit_data(ireq);
  1541. if (status != SCI_SUCCESS)
  1542. break;
  1543. sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_DATA_OUT);
  1544. }
  1545. break;
  1546. case FIS_SETDEVBITS:
  1547. sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
  1548. break;
  1549. case FIS_REGD2H:
  1550. if (frame_header->status & ATA_BUSY) {
  1551. /*
  1552. * Now why is the drive sending a D2H Register
  1553. * FIS when it is still busy? Do nothing since
  1554. * we are still in the right state.
  1555. */
  1556. dev_dbg(&ihost->pdev->dev,
  1557. "%s: SCIC PIO Request 0x%p received "
  1558. "D2H Register FIS with BSY status "
  1559. "0x%x\n",
  1560. __func__,
  1561. stp_req,
  1562. frame_header->status);
  1563. break;
  1564. }
  1565. sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
  1566. frame_index,
  1567. (void **)&frame_buffer);
  1568. sci_controller_copy_sata_response(&ireq->stp.req,
  1569. frame_header,
  1570. frame_buffer);
  1571. ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
  1572. ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
  1573. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1574. break;
  1575. default:
  1576. /* FIXME: what do we do here? */
  1577. break;
  1578. }
  1579. /* Frame is decoded return it to the controller */
  1580. sci_controller_release_frame(ihost, frame_index);
  1581. return status;
  1582. }
  1583. case SCI_REQ_STP_PIO_DATA_IN: {
  1584. struct dev_to_host_fis *frame_header;
  1585. struct sata_fis_data *frame_buffer;
  1586. status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
  1587. frame_index,
  1588. (void **)&frame_header);
  1589. if (status != SCI_SUCCESS) {
  1590. dev_err(&ihost->pdev->dev,
  1591. "%s: SCIC IO Request 0x%p could not get frame "
  1592. "header for frame index %d, status %x\n",
  1593. __func__,
  1594. stp_req,
  1595. frame_index,
  1596. status);
  1597. return status;
  1598. }
  1599. if (frame_header->fis_type != FIS_DATA) {
  1600. dev_err(&ihost->pdev->dev,
  1601. "%s: SCIC PIO Request 0x%p received frame %d "
  1602. "with fis type 0x%02x when expecting a data "
  1603. "fis.\n",
  1604. __func__,
  1605. stp_req,
  1606. frame_index,
  1607. frame_header->fis_type);
  1608. ireq->scu_status = SCU_TASK_DONE_GOOD;
  1609. ireq->sci_status = SCI_FAILURE_IO_REQUIRES_SCSI_ABORT;
  1610. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1611. /* Frame is decoded return it to the controller */
  1612. sci_controller_release_frame(ihost, frame_index);
  1613. return status;
  1614. }
  1615. if (stp_req->sgl.index < 0) {
  1616. ireq->saved_rx_frame_index = frame_index;
  1617. stp_req->pio_len = 0;
  1618. } else {
  1619. sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
  1620. frame_index,
  1621. (void **)&frame_buffer);
  1622. status = sci_stp_request_pio_data_in_copy_data(stp_req,
  1623. (u8 *)frame_buffer);
  1624. /* Frame is decoded return it to the controller */
  1625. sci_controller_release_frame(ihost, frame_index);
  1626. }
  1627. /* Check for the end of the transfer, are there more
  1628. * bytes remaining for this data transfer
  1629. */
  1630. if (status != SCI_SUCCESS || stp_req->pio_len != 0)
  1631. return status;
  1632. if ((stp_req->status & ATA_BUSY) == 0) {
  1633. ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
  1634. ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
  1635. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1636. } else {
  1637. sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
  1638. }
  1639. return status;
  1640. }
  1641. case SCI_REQ_STP_SOFT_RESET_WAIT_D2H: {
  1642. struct dev_to_host_fis *frame_header;
  1643. u32 *frame_buffer;
  1644. status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
  1645. frame_index,
  1646. (void **)&frame_header);
  1647. if (status != SCI_SUCCESS) {
  1648. dev_err(&ihost->pdev->dev,
  1649. "%s: SCIC IO Request 0x%p could not get frame "
  1650. "header for frame index %d, status %x\n",
  1651. __func__,
  1652. stp_req,
  1653. frame_index,
  1654. status);
  1655. return status;
  1656. }
  1657. switch (frame_header->fis_type) {
  1658. case FIS_REGD2H:
  1659. sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
  1660. frame_index,
  1661. (void **)&frame_buffer);
  1662. sci_controller_copy_sata_response(&ireq->stp.rsp,
  1663. frame_header,
  1664. frame_buffer);
  1665. /* The command has completed with error */
  1666. ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
  1667. ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
  1668. break;
  1669. default:
  1670. dev_warn(&ihost->pdev->dev,
  1671. "%s: IO Request:0x%p Frame Id:%d protocol "
  1672. "violation occurred\n",
  1673. __func__,
  1674. stp_req,
  1675. frame_index);
  1676. ireq->scu_status = SCU_TASK_DONE_UNEXP_FIS;
  1677. ireq->sci_status = SCI_FAILURE_PROTOCOL_VIOLATION;
  1678. break;
  1679. }
  1680. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1681. /* Frame has been decoded return it to the controller */
  1682. sci_controller_release_frame(ihost, frame_index);
  1683. return status;
  1684. }
  1685. case SCI_REQ_ATAPI_WAIT_PIO_SETUP: {
  1686. struct sas_task *task = isci_request_access_task(ireq);
  1687. sci_controller_release_frame(ihost, frame_index);
  1688. ireq->target_device->working_request = ireq;
  1689. if (task->data_dir == DMA_NONE) {
  1690. sci_change_state(&ireq->sm, SCI_REQ_ATAPI_WAIT_TC_COMP);
  1691. scu_atapi_reconstruct_raw_frame_task_context(ireq);
  1692. } else {
  1693. sci_change_state(&ireq->sm, SCI_REQ_ATAPI_WAIT_D2H);
  1694. scu_atapi_construct_task_context(ireq);
  1695. }
  1696. sci_controller_continue_io(ireq);
  1697. return SCI_SUCCESS;
  1698. }
  1699. case SCI_REQ_ATAPI_WAIT_D2H:
  1700. return atapi_d2h_reg_frame_handler(ireq, frame_index);
  1701. case SCI_REQ_ABORTING:
  1702. /*
  1703. * TODO: Is it even possible to get an unsolicited frame in the
  1704. * aborting state?
  1705. */
  1706. sci_controller_release_frame(ihost, frame_index);
  1707. return SCI_SUCCESS;
  1708. default:
  1709. dev_warn(&ihost->pdev->dev,
  1710. "%s: SCIC IO Request given unexpected frame %x while "
  1711. "in state %d\n",
  1712. __func__,
  1713. frame_index,
  1714. state);
  1715. sci_controller_release_frame(ihost, frame_index);
  1716. return SCI_FAILURE_INVALID_STATE;
  1717. }
  1718. }
  1719. static enum sci_status stp_request_udma_await_tc_event(struct isci_request *ireq,
  1720. u32 completion_code)
  1721. {
  1722. enum sci_status status = SCI_SUCCESS;
  1723. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1724. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1725. ireq->scu_status = SCU_TASK_DONE_GOOD;
  1726. ireq->sci_status = SCI_SUCCESS;
  1727. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1728. break;
  1729. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_FIS):
  1730. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
  1731. /* We must check ther response buffer to see if the D2H
  1732. * Register FIS was received before we got the TC
  1733. * completion.
  1734. */
  1735. if (ireq->stp.rsp.fis_type == FIS_REGD2H) {
  1736. sci_remote_device_suspend(ireq->target_device,
  1737. SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
  1738. ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
  1739. ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
  1740. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1741. } else {
  1742. /* If we have an error completion status for the
  1743. * TC then we can expect a D2H register FIS from
  1744. * the device so we must change state to wait
  1745. * for it
  1746. */
  1747. sci_change_state(&ireq->sm, SCI_REQ_STP_UDMA_WAIT_D2H);
  1748. }
  1749. break;
  1750. /* TODO Check to see if any of these completion status need to
  1751. * wait for the device to host register fis.
  1752. */
  1753. /* TODO We can retry the command for SCU_TASK_DONE_CMD_LL_R_ERR
  1754. * - this comes only for B0
  1755. */
  1756. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_INV_FIS_LEN):
  1757. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
  1758. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_R_ERR):
  1759. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CMD_LL_R_ERR):
  1760. sci_remote_device_suspend(ireq->target_device,
  1761. SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
  1762. /* Fall through to the default case */
  1763. default:
  1764. /* All other completion status cause the IO to be complete. */
  1765. ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
  1766. ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
  1767. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1768. break;
  1769. }
  1770. return status;
  1771. }
  1772. static enum sci_status
  1773. stp_request_soft_reset_await_h2d_asserted_tc_event(struct isci_request *ireq,
  1774. u32 completion_code)
  1775. {
  1776. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1777. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1778. ireq->scu_status = SCU_TASK_DONE_GOOD;
  1779. ireq->sci_status = SCI_SUCCESS;
  1780. sci_change_state(&ireq->sm, SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG);
  1781. break;
  1782. default:
  1783. /*
  1784. * All other completion status cause the IO to be complete.
  1785. * If a NAK was received, then it is up to the user to retry
  1786. * the request.
  1787. */
  1788. ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
  1789. ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
  1790. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1791. break;
  1792. }
  1793. return SCI_SUCCESS;
  1794. }
  1795. static enum sci_status
  1796. stp_request_soft_reset_await_h2d_diagnostic_tc_event(struct isci_request *ireq,
  1797. u32 completion_code)
  1798. {
  1799. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1800. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1801. ireq->scu_status = SCU_TASK_DONE_GOOD;
  1802. ireq->sci_status = SCI_SUCCESS;
  1803. sci_change_state(&ireq->sm, SCI_REQ_STP_SOFT_RESET_WAIT_D2H);
  1804. break;
  1805. default:
  1806. /* All other completion status cause the IO to be complete. If
  1807. * a NAK was received, then it is up to the user to retry the
  1808. * request.
  1809. */
  1810. ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
  1811. ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
  1812. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1813. break;
  1814. }
  1815. return SCI_SUCCESS;
  1816. }
  1817. static enum sci_status atapi_raw_completion(struct isci_request *ireq, u32 completion_code,
  1818. enum sci_base_request_states next)
  1819. {
  1820. enum sci_status status = SCI_SUCCESS;
  1821. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1822. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1823. ireq->scu_status = SCU_TASK_DONE_GOOD;
  1824. ireq->sci_status = SCI_SUCCESS;
  1825. sci_change_state(&ireq->sm, next);
  1826. break;
  1827. default:
  1828. /* All other completion status cause the IO to be complete.
  1829. * If a NAK was received, then it is up to the user to retry
  1830. * the request.
  1831. */
  1832. ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
  1833. ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
  1834. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1835. break;
  1836. }
  1837. return status;
  1838. }
  1839. static enum sci_status atapi_data_tc_completion_handler(struct isci_request *ireq,
  1840. u32 completion_code)
  1841. {
  1842. struct isci_remote_device *idev = ireq->target_device;
  1843. struct dev_to_host_fis *d2h = &ireq->stp.rsp;
  1844. enum sci_status status = SCI_SUCCESS;
  1845. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1846. case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
  1847. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1848. break;
  1849. case (SCU_TASK_DONE_UNEXP_FIS << SCU_COMPLETION_TL_STATUS_SHIFT): {
  1850. u16 len = sci_req_tx_bytes(ireq);
  1851. /* likely non-error data underrrun, workaround missing
  1852. * d2h frame from the controller
  1853. */
  1854. if (d2h->fis_type != FIS_REGD2H) {
  1855. d2h->fis_type = FIS_REGD2H;
  1856. d2h->flags = (1 << 6);
  1857. d2h->status = 0x50;
  1858. d2h->error = 0;
  1859. d2h->lbal = 0;
  1860. d2h->byte_count_low = len & 0xff;
  1861. d2h->byte_count_high = len >> 8;
  1862. d2h->device = 0xa0;
  1863. d2h->lbal_exp = 0;
  1864. d2h->lbam_exp = 0;
  1865. d2h->lbah_exp = 0;
  1866. d2h->_r_a = 0;
  1867. d2h->sector_count = 0x3;
  1868. d2h->sector_count_exp = 0;
  1869. d2h->_r_b = 0;
  1870. d2h->_r_c = 0;
  1871. d2h->_r_d = 0;
  1872. }
  1873. ireq->scu_status = SCU_TASK_DONE_GOOD;
  1874. ireq->sci_status = SCI_SUCCESS_IO_DONE_EARLY;
  1875. status = ireq->sci_status;
  1876. /* the hw will have suspended the rnc, so complete the
  1877. * request upon pending resume
  1878. */
  1879. sci_change_state(&idev->sm, SCI_STP_DEV_ATAPI_ERROR);
  1880. break;
  1881. }
  1882. case (SCU_TASK_DONE_EXCESS_DATA << SCU_COMPLETION_TL_STATUS_SHIFT):
  1883. /* In this case, there is no UF coming after.
  1884. * compelte the IO now.
  1885. */
  1886. ireq->scu_status = SCU_TASK_DONE_GOOD;
  1887. ireq->sci_status = SCI_SUCCESS;
  1888. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  1889. break;
  1890. default:
  1891. if (d2h->fis_type == FIS_REGD2H) {
  1892. /* UF received change the device state to ATAPI_ERROR */
  1893. status = ireq->sci_status;
  1894. sci_change_state(&idev->sm, SCI_STP_DEV_ATAPI_ERROR);
  1895. } else {
  1896. /* If receiving any non-sucess TC status, no UF
  1897. * received yet, then an UF for the status fis
  1898. * is coming after (XXX: suspect this is
  1899. * actually a protocol error or a bug like the
  1900. * DONE_UNEXP_FIS case)
  1901. */
  1902. ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
  1903. ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
  1904. sci_change_state(&ireq->sm, SCI_REQ_ATAPI_WAIT_D2H);
  1905. }
  1906. break;
  1907. }
  1908. return status;
  1909. }
  1910. enum sci_status
  1911. sci_io_request_tc_completion(struct isci_request *ireq,
  1912. u32 completion_code)
  1913. {
  1914. enum sci_base_request_states state;
  1915. struct isci_host *ihost = ireq->owning_controller;
  1916. state = ireq->sm.current_state_id;
  1917. switch (state) {
  1918. case SCI_REQ_STARTED:
  1919. return request_started_state_tc_event(ireq, completion_code);
  1920. case SCI_REQ_TASK_WAIT_TC_COMP:
  1921. return ssp_task_request_await_tc_event(ireq,
  1922. completion_code);
  1923. case SCI_REQ_SMP_WAIT_RESP:
  1924. return smp_request_await_response_tc_event(ireq,
  1925. completion_code);
  1926. case SCI_REQ_SMP_WAIT_TC_COMP:
  1927. return smp_request_await_tc_event(ireq, completion_code);
  1928. case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
  1929. return stp_request_udma_await_tc_event(ireq,
  1930. completion_code);
  1931. case SCI_REQ_STP_NON_DATA_WAIT_H2D:
  1932. return stp_request_non_data_await_h2d_tc_event(ireq,
  1933. completion_code);
  1934. case SCI_REQ_STP_PIO_WAIT_H2D:
  1935. return stp_request_pio_await_h2d_completion_tc_event(ireq,
  1936. completion_code);
  1937. case SCI_REQ_STP_PIO_DATA_OUT:
  1938. return pio_data_out_tx_done_tc_event(ireq, completion_code);
  1939. case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED:
  1940. return stp_request_soft_reset_await_h2d_asserted_tc_event(ireq,
  1941. completion_code);
  1942. case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG:
  1943. return stp_request_soft_reset_await_h2d_diagnostic_tc_event(ireq,
  1944. completion_code);
  1945. case SCI_REQ_ABORTING:
  1946. return request_aborting_state_tc_event(ireq,
  1947. completion_code);
  1948. case SCI_REQ_ATAPI_WAIT_H2D:
  1949. return atapi_raw_completion(ireq, completion_code,
  1950. SCI_REQ_ATAPI_WAIT_PIO_SETUP);
  1951. case SCI_REQ_ATAPI_WAIT_TC_COMP:
  1952. return atapi_raw_completion(ireq, completion_code,
  1953. SCI_REQ_ATAPI_WAIT_D2H);
  1954. case SCI_REQ_ATAPI_WAIT_D2H:
  1955. return atapi_data_tc_completion_handler(ireq, completion_code);
  1956. default:
  1957. dev_warn(&ihost->pdev->dev,
  1958. "%s: SCIC IO Request given task completion "
  1959. "notification %x while in wrong state %d\n",
  1960. __func__,
  1961. completion_code,
  1962. state);
  1963. return SCI_FAILURE_INVALID_STATE;
  1964. }
  1965. }
  1966. /**
  1967. * isci_request_process_response_iu() - This function sets the status and
  1968. * response iu, in the task struct, from the request object for the upper
  1969. * layer driver.
  1970. * @sas_task: This parameter is the task struct from the upper layer driver.
  1971. * @resp_iu: This parameter points to the response iu of the completed request.
  1972. * @dev: This parameter specifies the linux device struct.
  1973. *
  1974. * none.
  1975. */
  1976. static void isci_request_process_response_iu(
  1977. struct sas_task *task,
  1978. struct ssp_response_iu *resp_iu,
  1979. struct device *dev)
  1980. {
  1981. dev_dbg(dev,
  1982. "%s: resp_iu = %p "
  1983. "resp_iu->status = 0x%x,\nresp_iu->datapres = %d "
  1984. "resp_iu->response_data_len = %x, "
  1985. "resp_iu->sense_data_len = %x\nrepsonse data: ",
  1986. __func__,
  1987. resp_iu,
  1988. resp_iu->status,
  1989. resp_iu->datapres,
  1990. resp_iu->response_data_len,
  1991. resp_iu->sense_data_len);
  1992. task->task_status.stat = resp_iu->status;
  1993. /* libsas updates the task status fields based on the response iu. */
  1994. sas_ssp_task_response(dev, task, resp_iu);
  1995. }
  1996. /**
  1997. * isci_request_set_open_reject_status() - This function prepares the I/O
  1998. * completion for OPEN_REJECT conditions.
  1999. * @request: This parameter is the completed isci_request object.
  2000. * @response_ptr: This parameter specifies the service response for the I/O.
  2001. * @status_ptr: This parameter specifies the exec status for the I/O.
  2002. * @complete_to_host_ptr: This parameter specifies the action to be taken by
  2003. * the LLDD with respect to completing this request or forcing an abort
  2004. * condition on the I/O.
  2005. * @open_rej_reason: This parameter specifies the encoded reason for the
  2006. * abandon-class reject.
  2007. *
  2008. * none.
  2009. */
  2010. static void isci_request_set_open_reject_status(
  2011. struct isci_request *request,
  2012. struct sas_task *task,
  2013. enum service_response *response_ptr,
  2014. enum exec_status *status_ptr,
  2015. enum isci_completion_selection *complete_to_host_ptr,
  2016. enum sas_open_rej_reason open_rej_reason)
  2017. {
  2018. /* Task in the target is done. */
  2019. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2020. *response_ptr = SAS_TASK_UNDELIVERED;
  2021. *status_ptr = SAS_OPEN_REJECT;
  2022. *complete_to_host_ptr = isci_perform_normal_io_completion;
  2023. task->task_status.open_rej_reason = open_rej_reason;
  2024. }
  2025. /**
  2026. * isci_request_handle_controller_specific_errors() - This function decodes
  2027. * controller-specific I/O completion error conditions.
  2028. * @request: This parameter is the completed isci_request object.
  2029. * @response_ptr: This parameter specifies the service response for the I/O.
  2030. * @status_ptr: This parameter specifies the exec status for the I/O.
  2031. * @complete_to_host_ptr: This parameter specifies the action to be taken by
  2032. * the LLDD with respect to completing this request or forcing an abort
  2033. * condition on the I/O.
  2034. *
  2035. * none.
  2036. */
  2037. static void isci_request_handle_controller_specific_errors(
  2038. struct isci_remote_device *idev,
  2039. struct isci_request *request,
  2040. struct sas_task *task,
  2041. enum service_response *response_ptr,
  2042. enum exec_status *status_ptr,
  2043. enum isci_completion_selection *complete_to_host_ptr)
  2044. {
  2045. unsigned int cstatus;
  2046. cstatus = request->scu_status;
  2047. dev_dbg(&request->isci_host->pdev->dev,
  2048. "%s: %p SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR "
  2049. "- controller status = 0x%x\n",
  2050. __func__, request, cstatus);
  2051. /* Decode the controller-specific errors; most
  2052. * important is to recognize those conditions in which
  2053. * the target may still have a task outstanding that
  2054. * must be aborted.
  2055. *
  2056. * Note that there are SCU completion codes being
  2057. * named in the decode below for which SCIC has already
  2058. * done work to handle them in a way other than as
  2059. * a controller-specific completion code; these are left
  2060. * in the decode below for completeness sake.
  2061. */
  2062. switch (cstatus) {
  2063. case SCU_TASK_DONE_DMASETUP_DIRERR:
  2064. /* Also SCU_TASK_DONE_SMP_FRM_TYPE_ERR: */
  2065. case SCU_TASK_DONE_XFERCNT_ERR:
  2066. /* Also SCU_TASK_DONE_SMP_UFI_ERR: */
  2067. if (task->task_proto == SAS_PROTOCOL_SMP) {
  2068. /* SCU_TASK_DONE_SMP_UFI_ERR == Task Done. */
  2069. *response_ptr = SAS_TASK_COMPLETE;
  2070. /* See if the device has been/is being stopped. Note
  2071. * that we ignore the quiesce state, since we are
  2072. * concerned about the actual device state.
  2073. */
  2074. if (!idev)
  2075. *status_ptr = SAS_DEVICE_UNKNOWN;
  2076. else
  2077. *status_ptr = SAS_ABORTED_TASK;
  2078. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2079. *complete_to_host_ptr =
  2080. isci_perform_normal_io_completion;
  2081. } else {
  2082. /* Task in the target is not done. */
  2083. *response_ptr = SAS_TASK_UNDELIVERED;
  2084. if (!idev)
  2085. *status_ptr = SAS_DEVICE_UNKNOWN;
  2086. else
  2087. *status_ptr = SAM_STAT_TASK_ABORTED;
  2088. clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2089. *complete_to_host_ptr =
  2090. isci_perform_error_io_completion;
  2091. }
  2092. break;
  2093. case SCU_TASK_DONE_CRC_ERR:
  2094. case SCU_TASK_DONE_NAK_CMD_ERR:
  2095. case SCU_TASK_DONE_EXCESS_DATA:
  2096. case SCU_TASK_DONE_UNEXP_FIS:
  2097. /* Also SCU_TASK_DONE_UNEXP_RESP: */
  2098. case SCU_TASK_DONE_VIIT_ENTRY_NV: /* TODO - conditions? */
  2099. case SCU_TASK_DONE_IIT_ENTRY_NV: /* TODO - conditions? */
  2100. case SCU_TASK_DONE_RNCNV_OUTBOUND: /* TODO - conditions? */
  2101. /* These are conditions in which the target
  2102. * has completed the task, so that no cleanup
  2103. * is necessary.
  2104. */
  2105. *response_ptr = SAS_TASK_COMPLETE;
  2106. /* See if the device has been/is being stopped. Note
  2107. * that we ignore the quiesce state, since we are
  2108. * concerned about the actual device state.
  2109. */
  2110. if (!idev)
  2111. *status_ptr = SAS_DEVICE_UNKNOWN;
  2112. else
  2113. *status_ptr = SAS_ABORTED_TASK;
  2114. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2115. *complete_to_host_ptr = isci_perform_normal_io_completion;
  2116. break;
  2117. /* Note that the only open reject completion codes seen here will be
  2118. * abandon-class codes; all others are automatically retried in the SCU.
  2119. */
  2120. case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
  2121. isci_request_set_open_reject_status(
  2122. request, task, response_ptr, status_ptr,
  2123. complete_to_host_ptr, SAS_OREJ_WRONG_DEST);
  2124. break;
  2125. case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
  2126. /* Note - the return of AB0 will change when
  2127. * libsas implements detection of zone violations.
  2128. */
  2129. isci_request_set_open_reject_status(
  2130. request, task, response_ptr, status_ptr,
  2131. complete_to_host_ptr, SAS_OREJ_RESV_AB0);
  2132. break;
  2133. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
  2134. isci_request_set_open_reject_status(
  2135. request, task, response_ptr, status_ptr,
  2136. complete_to_host_ptr, SAS_OREJ_RESV_AB1);
  2137. break;
  2138. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
  2139. isci_request_set_open_reject_status(
  2140. request, task, response_ptr, status_ptr,
  2141. complete_to_host_ptr, SAS_OREJ_RESV_AB2);
  2142. break;
  2143. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
  2144. isci_request_set_open_reject_status(
  2145. request, task, response_ptr, status_ptr,
  2146. complete_to_host_ptr, SAS_OREJ_RESV_AB3);
  2147. break;
  2148. case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
  2149. isci_request_set_open_reject_status(
  2150. request, task, response_ptr, status_ptr,
  2151. complete_to_host_ptr, SAS_OREJ_BAD_DEST);
  2152. break;
  2153. case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
  2154. isci_request_set_open_reject_status(
  2155. request, task, response_ptr, status_ptr,
  2156. complete_to_host_ptr, SAS_OREJ_STP_NORES);
  2157. break;
  2158. case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
  2159. isci_request_set_open_reject_status(
  2160. request, task, response_ptr, status_ptr,
  2161. complete_to_host_ptr, SAS_OREJ_EPROTO);
  2162. break;
  2163. case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
  2164. isci_request_set_open_reject_status(
  2165. request, task, response_ptr, status_ptr,
  2166. complete_to_host_ptr, SAS_OREJ_CONN_RATE);
  2167. break;
  2168. case SCU_TASK_DONE_LL_R_ERR:
  2169. /* Also SCU_TASK_DONE_ACK_NAK_TO: */
  2170. case SCU_TASK_DONE_LL_PERR:
  2171. case SCU_TASK_DONE_LL_SY_TERM:
  2172. /* Also SCU_TASK_DONE_NAK_ERR:*/
  2173. case SCU_TASK_DONE_LL_LF_TERM:
  2174. /* Also SCU_TASK_DONE_DATA_LEN_ERR: */
  2175. case SCU_TASK_DONE_LL_ABORT_ERR:
  2176. case SCU_TASK_DONE_SEQ_INV_TYPE:
  2177. /* Also SCU_TASK_DONE_UNEXP_XR: */
  2178. case SCU_TASK_DONE_XR_IU_LEN_ERR:
  2179. case SCU_TASK_DONE_INV_FIS_LEN:
  2180. /* Also SCU_TASK_DONE_XR_WD_LEN: */
  2181. case SCU_TASK_DONE_SDMA_ERR:
  2182. case SCU_TASK_DONE_OFFSET_ERR:
  2183. case SCU_TASK_DONE_MAX_PLD_ERR:
  2184. case SCU_TASK_DONE_LF_ERR:
  2185. case SCU_TASK_DONE_SMP_RESP_TO_ERR: /* Escalate to dev reset? */
  2186. case SCU_TASK_DONE_SMP_LL_RX_ERR:
  2187. case SCU_TASK_DONE_UNEXP_DATA:
  2188. case SCU_TASK_DONE_UNEXP_SDBFIS:
  2189. case SCU_TASK_DONE_REG_ERR:
  2190. case SCU_TASK_DONE_SDB_ERR:
  2191. case SCU_TASK_DONE_TASK_ABORT:
  2192. default:
  2193. /* Task in the target is not done. */
  2194. *response_ptr = SAS_TASK_UNDELIVERED;
  2195. *status_ptr = SAM_STAT_TASK_ABORTED;
  2196. if (task->task_proto == SAS_PROTOCOL_SMP) {
  2197. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2198. *complete_to_host_ptr = isci_perform_normal_io_completion;
  2199. } else {
  2200. clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2201. *complete_to_host_ptr = isci_perform_error_io_completion;
  2202. }
  2203. break;
  2204. }
  2205. }
  2206. /**
  2207. * isci_task_save_for_upper_layer_completion() - This function saves the
  2208. * request for later completion to the upper layer driver.
  2209. * @host: This parameter is a pointer to the host on which the the request
  2210. * should be queued (either as an error or success).
  2211. * @request: This parameter is the completed request.
  2212. * @response: This parameter is the response code for the completed task.
  2213. * @status: This parameter is the status code for the completed task.
  2214. *
  2215. * none.
  2216. */
  2217. static void isci_task_save_for_upper_layer_completion(
  2218. struct isci_host *host,
  2219. struct isci_request *request,
  2220. enum service_response response,
  2221. enum exec_status status,
  2222. enum isci_completion_selection task_notification_selection)
  2223. {
  2224. struct sas_task *task = isci_request_access_task(request);
  2225. task_notification_selection
  2226. = isci_task_set_completion_status(task, response, status,
  2227. task_notification_selection);
  2228. /* Tasks aborted specifically by a call to the lldd_abort_task
  2229. * function should not be completed to the host in the regular path.
  2230. */
  2231. switch (task_notification_selection) {
  2232. case isci_perform_normal_io_completion:
  2233. /* Normal notification (task_done) */
  2234. /* Add to the completed list. */
  2235. list_add(&request->completed_node,
  2236. &host->requests_to_complete);
  2237. /* Take the request off the device's pending request list. */
  2238. list_del_init(&request->dev_node);
  2239. break;
  2240. case isci_perform_aborted_io_completion:
  2241. /* No notification to libsas because this request is
  2242. * already in the abort path.
  2243. */
  2244. /* Wake up whatever process was waiting for this
  2245. * request to complete.
  2246. */
  2247. WARN_ON(request->io_request_completion == NULL);
  2248. if (request->io_request_completion != NULL) {
  2249. /* Signal whoever is waiting that this
  2250. * request is complete.
  2251. */
  2252. complete(request->io_request_completion);
  2253. }
  2254. break;
  2255. case isci_perform_error_io_completion:
  2256. /* Use sas_task_abort */
  2257. /* Add to the aborted list. */
  2258. list_add(&request->completed_node,
  2259. &host->requests_to_errorback);
  2260. break;
  2261. default:
  2262. /* Add to the error to libsas list. */
  2263. list_add(&request->completed_node,
  2264. &host->requests_to_errorback);
  2265. break;
  2266. }
  2267. dev_dbg(&host->pdev->dev,
  2268. "%s: %d - task = %p, response=%d (%d), status=%d (%d)\n",
  2269. __func__, task_notification_selection, task,
  2270. (task) ? task->task_status.resp : 0, response,
  2271. (task) ? task->task_status.stat : 0, status);
  2272. }
  2273. static void isci_process_stp_response(struct sas_task *task, struct dev_to_host_fis *fis)
  2274. {
  2275. struct task_status_struct *ts = &task->task_status;
  2276. struct ata_task_resp *resp = (void *)&ts->buf[0];
  2277. resp->frame_len = sizeof(*fis);
  2278. memcpy(resp->ending_fis, fis, sizeof(*fis));
  2279. ts->buf_valid_size = sizeof(*resp);
  2280. /* If the device fault bit is set in the status register, then
  2281. * set the sense data and return.
  2282. */
  2283. if (fis->status & ATA_DF)
  2284. ts->stat = SAS_PROTO_RESPONSE;
  2285. else if (fis->status & ATA_ERR)
  2286. ts->stat = SAM_STAT_CHECK_CONDITION;
  2287. else
  2288. ts->stat = SAM_STAT_GOOD;
  2289. ts->resp = SAS_TASK_COMPLETE;
  2290. }
  2291. static void isci_request_io_request_complete(struct isci_host *ihost,
  2292. struct isci_request *request,
  2293. enum sci_io_status completion_status)
  2294. {
  2295. struct sas_task *task = isci_request_access_task(request);
  2296. struct ssp_response_iu *resp_iu;
  2297. unsigned long task_flags;
  2298. struct isci_remote_device *idev = request->target_device;
  2299. enum service_response response = SAS_TASK_UNDELIVERED;
  2300. enum exec_status status = SAS_ABORTED_TASK;
  2301. enum isci_request_status request_status;
  2302. enum isci_completion_selection complete_to_host
  2303. = isci_perform_normal_io_completion;
  2304. dev_dbg(&ihost->pdev->dev,
  2305. "%s: request = %p, task = %p,\n"
  2306. "task->data_dir = %d completion_status = 0x%x\n",
  2307. __func__,
  2308. request,
  2309. task,
  2310. task->data_dir,
  2311. completion_status);
  2312. spin_lock(&request->state_lock);
  2313. request_status = request->status;
  2314. /* Decode the request status. Note that if the request has been
  2315. * aborted by a task management function, we don't care
  2316. * what the status is.
  2317. */
  2318. switch (request_status) {
  2319. case aborted:
  2320. /* "aborted" indicates that the request was aborted by a task
  2321. * management function, since once a task management request is
  2322. * perfomed by the device, the request only completes because
  2323. * of the subsequent driver terminate.
  2324. *
  2325. * Aborted also means an external thread is explicitly managing
  2326. * this request, so that we do not complete it up the stack.
  2327. *
  2328. * The target is still there (since the TMF was successful).
  2329. */
  2330. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2331. response = SAS_TASK_COMPLETE;
  2332. /* See if the device has been/is being stopped. Note
  2333. * that we ignore the quiesce state, since we are
  2334. * concerned about the actual device state.
  2335. */
  2336. if (!idev)
  2337. status = SAS_DEVICE_UNKNOWN;
  2338. else
  2339. status = SAS_ABORTED_TASK;
  2340. complete_to_host = isci_perform_aborted_io_completion;
  2341. /* This was an aborted request. */
  2342. spin_unlock(&request->state_lock);
  2343. break;
  2344. case aborting:
  2345. /* aborting means that the task management function tried and
  2346. * failed to abort the request. We need to note the request
  2347. * as SAS_TASK_UNDELIVERED, so that the scsi mid layer marks the
  2348. * target as down.
  2349. *
  2350. * Aborting also means an external thread is explicitly managing
  2351. * this request, so that we do not complete it up the stack.
  2352. */
  2353. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2354. response = SAS_TASK_UNDELIVERED;
  2355. if (!idev)
  2356. /* The device has been /is being stopped. Note that
  2357. * we ignore the quiesce state, since we are
  2358. * concerned about the actual device state.
  2359. */
  2360. status = SAS_DEVICE_UNKNOWN;
  2361. else
  2362. status = SAS_PHY_DOWN;
  2363. complete_to_host = isci_perform_aborted_io_completion;
  2364. /* This was an aborted request. */
  2365. spin_unlock(&request->state_lock);
  2366. break;
  2367. case terminating:
  2368. /* This was an terminated request. This happens when
  2369. * the I/O is being terminated because of an action on
  2370. * the device (reset, tear down, etc.), and the I/O needs
  2371. * to be completed up the stack.
  2372. */
  2373. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2374. response = SAS_TASK_UNDELIVERED;
  2375. /* See if the device has been/is being stopped. Note
  2376. * that we ignore the quiesce state, since we are
  2377. * concerned about the actual device state.
  2378. */
  2379. if (!idev)
  2380. status = SAS_DEVICE_UNKNOWN;
  2381. else
  2382. status = SAS_ABORTED_TASK;
  2383. complete_to_host = isci_perform_aborted_io_completion;
  2384. /* This was a terminated request. */
  2385. spin_unlock(&request->state_lock);
  2386. break;
  2387. case dead:
  2388. /* This was a terminated request that timed-out during the
  2389. * termination process. There is no task to complete to
  2390. * libsas.
  2391. */
  2392. complete_to_host = isci_perform_normal_io_completion;
  2393. spin_unlock(&request->state_lock);
  2394. break;
  2395. default:
  2396. /* The request is done from an SCU HW perspective. */
  2397. request->status = completed;
  2398. spin_unlock(&request->state_lock);
  2399. /* This is an active request being completed from the core. */
  2400. switch (completion_status) {
  2401. case SCI_IO_FAILURE_RESPONSE_VALID:
  2402. dev_dbg(&ihost->pdev->dev,
  2403. "%s: SCI_IO_FAILURE_RESPONSE_VALID (%p/%p)\n",
  2404. __func__,
  2405. request,
  2406. task);
  2407. if (sas_protocol_ata(task->task_proto)) {
  2408. isci_process_stp_response(task, &request->stp.rsp);
  2409. } else if (SAS_PROTOCOL_SSP == task->task_proto) {
  2410. /* crack the iu response buffer. */
  2411. resp_iu = &request->ssp.rsp;
  2412. isci_request_process_response_iu(task, resp_iu,
  2413. &ihost->pdev->dev);
  2414. } else if (SAS_PROTOCOL_SMP == task->task_proto) {
  2415. dev_err(&ihost->pdev->dev,
  2416. "%s: SCI_IO_FAILURE_RESPONSE_VALID: "
  2417. "SAS_PROTOCOL_SMP protocol\n",
  2418. __func__);
  2419. } else
  2420. dev_err(&ihost->pdev->dev,
  2421. "%s: unknown protocol\n", __func__);
  2422. /* use the task status set in the task struct by the
  2423. * isci_request_process_response_iu call.
  2424. */
  2425. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2426. response = task->task_status.resp;
  2427. status = task->task_status.stat;
  2428. break;
  2429. case SCI_IO_SUCCESS:
  2430. case SCI_IO_SUCCESS_IO_DONE_EARLY:
  2431. response = SAS_TASK_COMPLETE;
  2432. status = SAM_STAT_GOOD;
  2433. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2434. if (completion_status == SCI_IO_SUCCESS_IO_DONE_EARLY) {
  2435. /* This was an SSP / STP / SATA transfer.
  2436. * There is a possibility that less data than
  2437. * the maximum was transferred.
  2438. */
  2439. u32 transferred_length = sci_req_tx_bytes(request);
  2440. task->task_status.residual
  2441. = task->total_xfer_len - transferred_length;
  2442. /* If there were residual bytes, call this an
  2443. * underrun.
  2444. */
  2445. if (task->task_status.residual != 0)
  2446. status = SAS_DATA_UNDERRUN;
  2447. dev_dbg(&ihost->pdev->dev,
  2448. "%s: SCI_IO_SUCCESS_IO_DONE_EARLY %d\n",
  2449. __func__,
  2450. status);
  2451. } else
  2452. dev_dbg(&ihost->pdev->dev,
  2453. "%s: SCI_IO_SUCCESS\n",
  2454. __func__);
  2455. break;
  2456. case SCI_IO_FAILURE_TERMINATED:
  2457. dev_dbg(&ihost->pdev->dev,
  2458. "%s: SCI_IO_FAILURE_TERMINATED (%p/%p)\n",
  2459. __func__,
  2460. request,
  2461. task);
  2462. /* The request was terminated explicitly. No handling
  2463. * is needed in the SCSI error handler path.
  2464. */
  2465. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2466. response = SAS_TASK_UNDELIVERED;
  2467. /* See if the device has been/is being stopped. Note
  2468. * that we ignore the quiesce state, since we are
  2469. * concerned about the actual device state.
  2470. */
  2471. if (!idev)
  2472. status = SAS_DEVICE_UNKNOWN;
  2473. else
  2474. status = SAS_ABORTED_TASK;
  2475. complete_to_host = isci_perform_normal_io_completion;
  2476. break;
  2477. case SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR:
  2478. isci_request_handle_controller_specific_errors(
  2479. idev, request, task, &response, &status,
  2480. &complete_to_host);
  2481. break;
  2482. case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
  2483. /* This is a special case, in that the I/O completion
  2484. * is telling us that the device needs a reset.
  2485. * In order for the device reset condition to be
  2486. * noticed, the I/O has to be handled in the error
  2487. * handler. Set the reset flag and cause the
  2488. * SCSI error thread to be scheduled.
  2489. */
  2490. spin_lock_irqsave(&task->task_state_lock, task_flags);
  2491. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  2492. spin_unlock_irqrestore(&task->task_state_lock, task_flags);
  2493. /* Fail the I/O. */
  2494. response = SAS_TASK_UNDELIVERED;
  2495. status = SAM_STAT_TASK_ABORTED;
  2496. complete_to_host = isci_perform_error_io_completion;
  2497. clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2498. break;
  2499. case SCI_FAILURE_RETRY_REQUIRED:
  2500. /* Fail the I/O so it can be retried. */
  2501. response = SAS_TASK_UNDELIVERED;
  2502. if (!idev)
  2503. status = SAS_DEVICE_UNKNOWN;
  2504. else
  2505. status = SAS_ABORTED_TASK;
  2506. complete_to_host = isci_perform_normal_io_completion;
  2507. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2508. break;
  2509. default:
  2510. /* Catch any otherwise unhandled error codes here. */
  2511. dev_dbg(&ihost->pdev->dev,
  2512. "%s: invalid completion code: 0x%x - "
  2513. "isci_request = %p\n",
  2514. __func__, completion_status, request);
  2515. response = SAS_TASK_UNDELIVERED;
  2516. /* See if the device has been/is being stopped. Note
  2517. * that we ignore the quiesce state, since we are
  2518. * concerned about the actual device state.
  2519. */
  2520. if (!idev)
  2521. status = SAS_DEVICE_UNKNOWN;
  2522. else
  2523. status = SAS_ABORTED_TASK;
  2524. if (SAS_PROTOCOL_SMP == task->task_proto) {
  2525. set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2526. complete_to_host = isci_perform_normal_io_completion;
  2527. } else {
  2528. clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
  2529. complete_to_host = isci_perform_error_io_completion;
  2530. }
  2531. break;
  2532. }
  2533. break;
  2534. }
  2535. switch (task->task_proto) {
  2536. case SAS_PROTOCOL_SSP:
  2537. if (task->data_dir == DMA_NONE)
  2538. break;
  2539. if (task->num_scatter == 0)
  2540. /* 0 indicates a single dma address */
  2541. dma_unmap_single(&ihost->pdev->dev,
  2542. request->zero_scatter_daddr,
  2543. task->total_xfer_len, task->data_dir);
  2544. else /* unmap the sgl dma addresses */
  2545. dma_unmap_sg(&ihost->pdev->dev, task->scatter,
  2546. request->num_sg_entries, task->data_dir);
  2547. break;
  2548. case SAS_PROTOCOL_SMP: {
  2549. struct scatterlist *sg = &task->smp_task.smp_req;
  2550. struct smp_req *smp_req;
  2551. void *kaddr;
  2552. dma_unmap_sg(&ihost->pdev->dev, sg, 1, DMA_TO_DEVICE);
  2553. /* need to swab it back in case the command buffer is re-used */
  2554. kaddr = kmap_atomic(sg_page(sg), KM_IRQ0);
  2555. smp_req = kaddr + sg->offset;
  2556. sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
  2557. kunmap_atomic(kaddr, KM_IRQ0);
  2558. break;
  2559. }
  2560. default:
  2561. break;
  2562. }
  2563. /* Put the completed request on the correct list */
  2564. isci_task_save_for_upper_layer_completion(ihost, request, response,
  2565. status, complete_to_host
  2566. );
  2567. /* complete the io request to the core. */
  2568. sci_controller_complete_io(ihost, request->target_device, request);
  2569. /* set terminated handle so it cannot be completed or
  2570. * terminated again, and to cause any calls into abort
  2571. * task to recognize the already completed case.
  2572. */
  2573. set_bit(IREQ_TERMINATED, &request->flags);
  2574. }
  2575. static void sci_request_started_state_enter(struct sci_base_state_machine *sm)
  2576. {
  2577. struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
  2578. struct domain_device *dev = ireq->target_device->domain_dev;
  2579. enum sci_base_request_states state;
  2580. struct sas_task *task;
  2581. /* XXX as hch said always creating an internal sas_task for tmf
  2582. * requests would simplify the driver
  2583. */
  2584. task = (test_bit(IREQ_TMF, &ireq->flags)) ? NULL : isci_request_access_task(ireq);
  2585. /* all unaccelerated request types (non ssp or ncq) handled with
  2586. * substates
  2587. */
  2588. if (!task && dev->dev_type == SAS_END_DEV) {
  2589. state = SCI_REQ_TASK_WAIT_TC_COMP;
  2590. } else if (!task &&
  2591. (isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_high ||
  2592. isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_low)) {
  2593. state = SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED;
  2594. } else if (task && task->task_proto == SAS_PROTOCOL_SMP) {
  2595. state = SCI_REQ_SMP_WAIT_RESP;
  2596. } else if (task && sas_protocol_ata(task->task_proto) &&
  2597. !task->ata_task.use_ncq) {
  2598. if (dev->sata_dev.command_set == ATAPI_COMMAND_SET &&
  2599. task->ata_task.fis.command == ATA_CMD_PACKET) {
  2600. state = SCI_REQ_ATAPI_WAIT_H2D;
  2601. } else if (task->data_dir == DMA_NONE) {
  2602. state = SCI_REQ_STP_NON_DATA_WAIT_H2D;
  2603. } else if (task->ata_task.dma_xfer) {
  2604. state = SCI_REQ_STP_UDMA_WAIT_TC_COMP;
  2605. } else /* PIO */ {
  2606. state = SCI_REQ_STP_PIO_WAIT_H2D;
  2607. }
  2608. } else {
  2609. /* SSP or NCQ are fully accelerated, no substates */
  2610. return;
  2611. }
  2612. sci_change_state(sm, state);
  2613. }
  2614. static void sci_request_completed_state_enter(struct sci_base_state_machine *sm)
  2615. {
  2616. struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
  2617. struct isci_host *ihost = ireq->owning_controller;
  2618. /* Tell the SCI_USER that the IO request is complete */
  2619. if (!test_bit(IREQ_TMF, &ireq->flags))
  2620. isci_request_io_request_complete(ihost, ireq,
  2621. ireq->sci_status);
  2622. else
  2623. isci_task_request_complete(ihost, ireq, ireq->sci_status);
  2624. }
  2625. static void sci_request_aborting_state_enter(struct sci_base_state_machine *sm)
  2626. {
  2627. struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
  2628. /* Setting the abort bit in the Task Context is required by the silicon. */
  2629. ireq->tc->abort = 1;
  2630. }
  2631. static void sci_stp_request_started_non_data_await_h2d_completion_enter(struct sci_base_state_machine *sm)
  2632. {
  2633. struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
  2634. ireq->target_device->working_request = ireq;
  2635. }
  2636. static void sci_stp_request_started_pio_await_h2d_completion_enter(struct sci_base_state_machine *sm)
  2637. {
  2638. struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
  2639. ireq->target_device->working_request = ireq;
  2640. }
  2641. static void sci_stp_request_started_soft_reset_await_h2d_asserted_completion_enter(struct sci_base_state_machine *sm)
  2642. {
  2643. struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
  2644. ireq->target_device->working_request = ireq;
  2645. }
  2646. static void sci_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter(struct sci_base_state_machine *sm)
  2647. {
  2648. struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
  2649. struct scu_task_context *tc = ireq->tc;
  2650. struct host_to_dev_fis *h2d_fis;
  2651. enum sci_status status;
  2652. /* Clear the SRST bit */
  2653. h2d_fis = &ireq->stp.cmd;
  2654. h2d_fis->control = 0;
  2655. /* Clear the TC control bit */
  2656. tc->control_frame = 0;
  2657. status = sci_controller_continue_io(ireq);
  2658. WARN_ONCE(status != SCI_SUCCESS, "isci: continue io failure\n");
  2659. }
  2660. static const struct sci_base_state sci_request_state_table[] = {
  2661. [SCI_REQ_INIT] = { },
  2662. [SCI_REQ_CONSTRUCTED] = { },
  2663. [SCI_REQ_STARTED] = {
  2664. .enter_state = sci_request_started_state_enter,
  2665. },
  2666. [SCI_REQ_STP_NON_DATA_WAIT_H2D] = {
  2667. .enter_state = sci_stp_request_started_non_data_await_h2d_completion_enter,
  2668. },
  2669. [SCI_REQ_STP_NON_DATA_WAIT_D2H] = { },
  2670. [SCI_REQ_STP_PIO_WAIT_H2D] = {
  2671. .enter_state = sci_stp_request_started_pio_await_h2d_completion_enter,
  2672. },
  2673. [SCI_REQ_STP_PIO_WAIT_FRAME] = { },
  2674. [SCI_REQ_STP_PIO_DATA_IN] = { },
  2675. [SCI_REQ_STP_PIO_DATA_OUT] = { },
  2676. [SCI_REQ_STP_UDMA_WAIT_TC_COMP] = { },
  2677. [SCI_REQ_STP_UDMA_WAIT_D2H] = { },
  2678. [SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED] = {
  2679. .enter_state = sci_stp_request_started_soft_reset_await_h2d_asserted_completion_enter,
  2680. },
  2681. [SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG] = {
  2682. .enter_state = sci_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter,
  2683. },
  2684. [SCI_REQ_STP_SOFT_RESET_WAIT_D2H] = { },
  2685. [SCI_REQ_TASK_WAIT_TC_COMP] = { },
  2686. [SCI_REQ_TASK_WAIT_TC_RESP] = { },
  2687. [SCI_REQ_SMP_WAIT_RESP] = { },
  2688. [SCI_REQ_SMP_WAIT_TC_COMP] = { },
  2689. [SCI_REQ_ATAPI_WAIT_H2D] = { },
  2690. [SCI_REQ_ATAPI_WAIT_PIO_SETUP] = { },
  2691. [SCI_REQ_ATAPI_WAIT_D2H] = { },
  2692. [SCI_REQ_ATAPI_WAIT_TC_COMP] = { },
  2693. [SCI_REQ_COMPLETED] = {
  2694. .enter_state = sci_request_completed_state_enter,
  2695. },
  2696. [SCI_REQ_ABORTING] = {
  2697. .enter_state = sci_request_aborting_state_enter,
  2698. },
  2699. [SCI_REQ_FINAL] = { },
  2700. };
  2701. static void
  2702. sci_general_request_construct(struct isci_host *ihost,
  2703. struct isci_remote_device *idev,
  2704. struct isci_request *ireq)
  2705. {
  2706. sci_init_sm(&ireq->sm, sci_request_state_table, SCI_REQ_INIT);
  2707. ireq->target_device = idev;
  2708. ireq->protocol = SCIC_NO_PROTOCOL;
  2709. ireq->saved_rx_frame_index = SCU_INVALID_FRAME_INDEX;
  2710. ireq->sci_status = SCI_SUCCESS;
  2711. ireq->scu_status = 0;
  2712. ireq->post_context = 0xFFFFFFFF;
  2713. }
  2714. static enum sci_status
  2715. sci_io_request_construct(struct isci_host *ihost,
  2716. struct isci_remote_device *idev,
  2717. struct isci_request *ireq)
  2718. {
  2719. struct domain_device *dev = idev->domain_dev;
  2720. enum sci_status status = SCI_SUCCESS;
  2721. /* Build the common part of the request */
  2722. sci_general_request_construct(ihost, idev, ireq);
  2723. if (idev->rnc.remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
  2724. return SCI_FAILURE_INVALID_REMOTE_DEVICE;
  2725. if (dev->dev_type == SAS_END_DEV)
  2726. /* pass */;
  2727. else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
  2728. memset(&ireq->stp.cmd, 0, sizeof(ireq->stp.cmd));
  2729. else if (dev_is_expander(dev))
  2730. /* pass */;
  2731. else
  2732. return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  2733. memset(ireq->tc, 0, offsetof(struct scu_task_context, sgl_pair_ab));
  2734. return status;
  2735. }
  2736. enum sci_status sci_task_request_construct(struct isci_host *ihost,
  2737. struct isci_remote_device *idev,
  2738. u16 io_tag, struct isci_request *ireq)
  2739. {
  2740. struct domain_device *dev = idev->domain_dev;
  2741. enum sci_status status = SCI_SUCCESS;
  2742. /* Build the common part of the request */
  2743. sci_general_request_construct(ihost, idev, ireq);
  2744. if (dev->dev_type == SAS_END_DEV ||
  2745. dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
  2746. set_bit(IREQ_TMF, &ireq->flags);
  2747. memset(ireq->tc, 0, sizeof(struct scu_task_context));
  2748. } else
  2749. status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  2750. return status;
  2751. }
  2752. static enum sci_status isci_request_ssp_request_construct(
  2753. struct isci_request *request)
  2754. {
  2755. enum sci_status status;
  2756. dev_dbg(&request->isci_host->pdev->dev,
  2757. "%s: request = %p\n",
  2758. __func__,
  2759. request);
  2760. status = sci_io_request_construct_basic_ssp(request);
  2761. return status;
  2762. }
  2763. static enum sci_status isci_request_stp_request_construct(struct isci_request *ireq)
  2764. {
  2765. struct sas_task *task = isci_request_access_task(ireq);
  2766. struct host_to_dev_fis *fis = &ireq->stp.cmd;
  2767. struct ata_queued_cmd *qc = task->uldd_task;
  2768. enum sci_status status;
  2769. dev_dbg(&ireq->isci_host->pdev->dev,
  2770. "%s: ireq = %p\n",
  2771. __func__,
  2772. ireq);
  2773. memcpy(fis, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
  2774. if (!task->ata_task.device_control_reg_update)
  2775. fis->flags |= 0x80;
  2776. fis->flags &= 0xF0;
  2777. status = sci_io_request_construct_basic_sata(ireq);
  2778. if (qc && (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
  2779. qc->tf.command == ATA_CMD_FPDMA_READ)) {
  2780. fis->sector_count = qc->tag << 3;
  2781. ireq->tc->type.stp.ncq_tag = qc->tag;
  2782. }
  2783. return status;
  2784. }
  2785. static enum sci_status
  2786. sci_io_request_construct_smp(struct device *dev,
  2787. struct isci_request *ireq,
  2788. struct sas_task *task)
  2789. {
  2790. struct scatterlist *sg = &task->smp_task.smp_req;
  2791. struct isci_remote_device *idev;
  2792. struct scu_task_context *task_context;
  2793. struct isci_port *iport;
  2794. struct smp_req *smp_req;
  2795. void *kaddr;
  2796. u8 req_len;
  2797. u32 cmd;
  2798. kaddr = kmap_atomic(sg_page(sg), KM_IRQ0);
  2799. smp_req = kaddr + sg->offset;
  2800. /*
  2801. * Look at the SMP requests' header fields; for certain SAS 1.x SMP
  2802. * functions under SAS 2.0, a zero request length really indicates
  2803. * a non-zero default length.
  2804. */
  2805. if (smp_req->req_len == 0) {
  2806. switch (smp_req->func) {
  2807. case SMP_DISCOVER:
  2808. case SMP_REPORT_PHY_ERR_LOG:
  2809. case SMP_REPORT_PHY_SATA:
  2810. case SMP_REPORT_ROUTE_INFO:
  2811. smp_req->req_len = 2;
  2812. break;
  2813. case SMP_CONF_ROUTE_INFO:
  2814. case SMP_PHY_CONTROL:
  2815. case SMP_PHY_TEST_FUNCTION:
  2816. smp_req->req_len = 9;
  2817. break;
  2818. /* Default - zero is a valid default for 2.0. */
  2819. }
  2820. }
  2821. req_len = smp_req->req_len;
  2822. sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
  2823. cmd = *(u32 *) smp_req;
  2824. kunmap_atomic(kaddr, KM_IRQ0);
  2825. if (!dma_map_sg(dev, sg, 1, DMA_TO_DEVICE))
  2826. return SCI_FAILURE;
  2827. ireq->protocol = SCIC_SMP_PROTOCOL;
  2828. /* byte swap the smp request. */
  2829. task_context = ireq->tc;
  2830. idev = ireq->target_device;
  2831. iport = idev->owning_port;
  2832. /*
  2833. * Fill in the TC with the its required data
  2834. * 00h
  2835. */
  2836. task_context->priority = 0;
  2837. task_context->initiator_request = 1;
  2838. task_context->connection_rate = idev->connection_rate;
  2839. task_context->protocol_engine_index = ISCI_PEG;
  2840. task_context->logical_port_index = iport->physical_port_index;
  2841. task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SMP;
  2842. task_context->abort = 0;
  2843. task_context->valid = SCU_TASK_CONTEXT_VALID;
  2844. task_context->context_type = SCU_TASK_CONTEXT_TYPE;
  2845. /* 04h */
  2846. task_context->remote_node_index = idev->rnc.remote_node_index;
  2847. task_context->command_code = 0;
  2848. task_context->task_type = SCU_TASK_TYPE_SMP_REQUEST;
  2849. /* 08h */
  2850. task_context->link_layer_control = 0;
  2851. task_context->do_not_dma_ssp_good_response = 1;
  2852. task_context->strict_ordering = 0;
  2853. task_context->control_frame = 1;
  2854. task_context->timeout_enable = 0;
  2855. task_context->block_guard_enable = 0;
  2856. /* 0ch */
  2857. task_context->address_modifier = 0;
  2858. /* 10h */
  2859. task_context->ssp_command_iu_length = req_len;
  2860. /* 14h */
  2861. task_context->transfer_length_bytes = 0;
  2862. /*
  2863. * 18h ~ 30h, protocol specific
  2864. * since commandIU has been build by framework at this point, we just
  2865. * copy the frist DWord from command IU to this location. */
  2866. memcpy(&task_context->type.smp, &cmd, sizeof(u32));
  2867. /*
  2868. * 40h
  2869. * "For SMP you could program it to zero. We would prefer that way
  2870. * so that done code will be consistent." - Venki
  2871. */
  2872. task_context->task_phase = 0;
  2873. ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  2874. (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  2875. (iport->physical_port_index <<
  2876. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
  2877. ISCI_TAG_TCI(ireq->io_tag));
  2878. /*
  2879. * Copy the physical address for the command buffer to the SCU Task
  2880. * Context command buffer should not contain command header.
  2881. */
  2882. task_context->command_iu_upper = upper_32_bits(sg_dma_address(sg));
  2883. task_context->command_iu_lower = lower_32_bits(sg_dma_address(sg) + sizeof(u32));
  2884. /* SMP response comes as UF, so no need to set response IU address. */
  2885. task_context->response_iu_upper = 0;
  2886. task_context->response_iu_lower = 0;
  2887. sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
  2888. return SCI_SUCCESS;
  2889. }
  2890. /*
  2891. * isci_smp_request_build() - This function builds the smp request.
  2892. * @ireq: This parameter points to the isci_request allocated in the
  2893. * request construct function.
  2894. *
  2895. * SCI_SUCCESS on successfull completion, or specific failure code.
  2896. */
  2897. static enum sci_status isci_smp_request_build(struct isci_request *ireq)
  2898. {
  2899. struct sas_task *task = isci_request_access_task(ireq);
  2900. struct device *dev = &ireq->isci_host->pdev->dev;
  2901. enum sci_status status = SCI_FAILURE;
  2902. status = sci_io_request_construct_smp(dev, ireq, task);
  2903. if (status != SCI_SUCCESS)
  2904. dev_dbg(&ireq->isci_host->pdev->dev,
  2905. "%s: failed with status = %d\n",
  2906. __func__,
  2907. status);
  2908. return status;
  2909. }
  2910. /**
  2911. * isci_io_request_build() - This function builds the io request object.
  2912. * @ihost: This parameter specifies the ISCI host object
  2913. * @request: This parameter points to the isci_request object allocated in the
  2914. * request construct function.
  2915. * @sci_device: This parameter is the handle for the sci core's remote device
  2916. * object that is the destination for this request.
  2917. *
  2918. * SCI_SUCCESS on successfull completion, or specific failure code.
  2919. */
  2920. static enum sci_status isci_io_request_build(struct isci_host *ihost,
  2921. struct isci_request *request,
  2922. struct isci_remote_device *idev)
  2923. {
  2924. enum sci_status status = SCI_SUCCESS;
  2925. struct sas_task *task = isci_request_access_task(request);
  2926. dev_dbg(&ihost->pdev->dev,
  2927. "%s: idev = 0x%p; request = %p, "
  2928. "num_scatter = %d\n",
  2929. __func__,
  2930. idev,
  2931. request,
  2932. task->num_scatter);
  2933. /* map the sgl addresses, if present.
  2934. * libata does the mapping for sata devices
  2935. * before we get the request.
  2936. */
  2937. if (task->num_scatter &&
  2938. !sas_protocol_ata(task->task_proto) &&
  2939. !(SAS_PROTOCOL_SMP & task->task_proto)) {
  2940. request->num_sg_entries = dma_map_sg(
  2941. &ihost->pdev->dev,
  2942. task->scatter,
  2943. task->num_scatter,
  2944. task->data_dir
  2945. );
  2946. if (request->num_sg_entries == 0)
  2947. return SCI_FAILURE_INSUFFICIENT_RESOURCES;
  2948. }
  2949. status = sci_io_request_construct(ihost, idev, request);
  2950. if (status != SCI_SUCCESS) {
  2951. dev_dbg(&ihost->pdev->dev,
  2952. "%s: failed request construct\n",
  2953. __func__);
  2954. return SCI_FAILURE;
  2955. }
  2956. switch (task->task_proto) {
  2957. case SAS_PROTOCOL_SMP:
  2958. status = isci_smp_request_build(request);
  2959. break;
  2960. case SAS_PROTOCOL_SSP:
  2961. status = isci_request_ssp_request_construct(request);
  2962. break;
  2963. case SAS_PROTOCOL_SATA:
  2964. case SAS_PROTOCOL_STP:
  2965. case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
  2966. status = isci_request_stp_request_construct(request);
  2967. break;
  2968. default:
  2969. dev_dbg(&ihost->pdev->dev,
  2970. "%s: unknown protocol\n", __func__);
  2971. return SCI_FAILURE;
  2972. }
  2973. return SCI_SUCCESS;
  2974. }
  2975. static struct isci_request *isci_request_from_tag(struct isci_host *ihost, u16 tag)
  2976. {
  2977. struct isci_request *ireq;
  2978. ireq = ihost->reqs[ISCI_TAG_TCI(tag)];
  2979. ireq->io_tag = tag;
  2980. ireq->io_request_completion = NULL;
  2981. ireq->flags = 0;
  2982. ireq->num_sg_entries = 0;
  2983. INIT_LIST_HEAD(&ireq->completed_node);
  2984. INIT_LIST_HEAD(&ireq->dev_node);
  2985. isci_request_change_state(ireq, allocated);
  2986. return ireq;
  2987. }
  2988. static struct isci_request *isci_io_request_from_tag(struct isci_host *ihost,
  2989. struct sas_task *task,
  2990. u16 tag)
  2991. {
  2992. struct isci_request *ireq;
  2993. ireq = isci_request_from_tag(ihost, tag);
  2994. ireq->ttype_ptr.io_task_ptr = task;
  2995. clear_bit(IREQ_TMF, &ireq->flags);
  2996. task->lldd_task = ireq;
  2997. return ireq;
  2998. }
  2999. struct isci_request *isci_tmf_request_from_tag(struct isci_host *ihost,
  3000. struct isci_tmf *isci_tmf,
  3001. u16 tag)
  3002. {
  3003. struct isci_request *ireq;
  3004. ireq = isci_request_from_tag(ihost, tag);
  3005. ireq->ttype_ptr.tmf_task_ptr = isci_tmf;
  3006. set_bit(IREQ_TMF, &ireq->flags);
  3007. return ireq;
  3008. }
  3009. int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
  3010. struct sas_task *task, u16 tag)
  3011. {
  3012. enum sci_status status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  3013. struct isci_request *ireq;
  3014. unsigned long flags;
  3015. int ret = 0;
  3016. /* do common allocation and init of request object. */
  3017. ireq = isci_io_request_from_tag(ihost, task, tag);
  3018. status = isci_io_request_build(ihost, ireq, idev);
  3019. if (status != SCI_SUCCESS) {
  3020. dev_dbg(&ihost->pdev->dev,
  3021. "%s: request_construct failed - status = 0x%x\n",
  3022. __func__,
  3023. status);
  3024. return status;
  3025. }
  3026. spin_lock_irqsave(&ihost->scic_lock, flags);
  3027. if (test_bit(IDEV_IO_NCQERROR, &idev->flags)) {
  3028. if (isci_task_is_ncq_recovery(task)) {
  3029. /* The device is in an NCQ recovery state. Issue the
  3030. * request on the task side. Note that it will
  3031. * complete on the I/O request side because the
  3032. * request was built that way (ie.
  3033. * ireq->is_task_management_request is false).
  3034. */
  3035. status = sci_controller_start_task(ihost,
  3036. idev,
  3037. ireq);
  3038. } else {
  3039. status = SCI_FAILURE;
  3040. }
  3041. } else {
  3042. /* send the request, let the core assign the IO TAG. */
  3043. status = sci_controller_start_io(ihost, idev,
  3044. ireq);
  3045. }
  3046. if (status != SCI_SUCCESS &&
  3047. status != SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  3048. dev_dbg(&ihost->pdev->dev,
  3049. "%s: failed request start (0x%x)\n",
  3050. __func__, status);
  3051. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  3052. return status;
  3053. }
  3054. /* Either I/O started OK, or the core has signaled that
  3055. * the device needs a target reset.
  3056. *
  3057. * In either case, hold onto the I/O for later.
  3058. *
  3059. * Update it's status and add it to the list in the
  3060. * remote device object.
  3061. */
  3062. list_add(&ireq->dev_node, &idev->reqs_in_process);
  3063. if (status == SCI_SUCCESS) {
  3064. isci_request_change_state(ireq, started);
  3065. } else {
  3066. /* The request did not really start in the
  3067. * hardware, so clear the request handle
  3068. * here so no terminations will be done.
  3069. */
  3070. set_bit(IREQ_TERMINATED, &ireq->flags);
  3071. isci_request_change_state(ireq, completed);
  3072. }
  3073. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  3074. if (status ==
  3075. SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  3076. /* Signal libsas that we need the SCSI error
  3077. * handler thread to work on this I/O and that
  3078. * we want a device reset.
  3079. */
  3080. spin_lock_irqsave(&task->task_state_lock, flags);
  3081. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  3082. spin_unlock_irqrestore(&task->task_state_lock, flags);
  3083. /* Cause this task to be scheduled in the SCSI error
  3084. * handler thread.
  3085. */
  3086. isci_execpath_callback(ihost, task,
  3087. sas_task_abort);
  3088. /* Change the status, since we are holding
  3089. * the I/O until it is managed by the SCSI
  3090. * error handler.
  3091. */
  3092. status = SCI_SUCCESS;
  3093. }
  3094. return ret;
  3095. }