request.c 102 KB

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