request.c 102 KB

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