request.c 110 KB

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