mtip32xx.c 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266
  1. /*
  2. * Driver for the Micron P320 SSD
  3. * Copyright (C) 2011 Micron Technology, Inc.
  4. *
  5. * Portions of this code were derived from works subjected to the
  6. * following copyright:
  7. * Copyright (C) 2009 Integrated Device Technology, Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. */
  20. #include <linux/pci.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/ata.h>
  23. #include <linux/delay.h>
  24. #include <linux/hdreg.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/random.h>
  27. #include <linux/smp.h>
  28. #include <linux/compat.h>
  29. #include <linux/fs.h>
  30. #include <linux/module.h>
  31. #include <linux/genhd.h>
  32. #include <linux/blkdev.h>
  33. #include <linux/bio.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/idr.h>
  36. #include <linux/kthread.h>
  37. #include <../drivers/ata/ahci.h>
  38. #include <linux/export.h>
  39. #include <linux/debugfs.h>
  40. #include "mtip32xx.h"
  41. #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
  42. #define HW_CMD_TBL_SZ (AHCI_CMD_TBL_HDR_SZ + (MTIP_MAX_SG * 16))
  43. #define HW_CMD_TBL_AR_SZ (HW_CMD_TBL_SZ * MTIP_MAX_COMMAND_SLOTS)
  44. #define HW_PORT_PRIV_DMA_SZ \
  45. (HW_CMD_SLOT_SZ + HW_CMD_TBL_AR_SZ + AHCI_RX_FIS_SZ)
  46. #define HOST_CAP_NZDMA (1 << 19)
  47. #define HOST_HSORG 0xFC
  48. #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
  49. #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
  50. #define HSORG_HWREV 0xFF00
  51. #define HSORG_STYLE 0x8
  52. #define HSORG_SLOTGROUPS 0x7
  53. #define PORT_COMMAND_ISSUE 0x38
  54. #define PORT_SDBV 0x7C
  55. #define PORT_OFFSET 0x100
  56. #define PORT_MEM_SIZE 0x80
  57. #define PORT_IRQ_ERR \
  58. (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
  59. PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
  60. PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
  61. PORT_IRQ_OVERFLOW)
  62. #define PORT_IRQ_LEGACY \
  63. (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
  64. #define PORT_IRQ_HANDLED \
  65. (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
  66. PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
  67. PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
  68. #define DEF_PORT_IRQ \
  69. (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
  70. /* product numbers */
  71. #define MTIP_PRODUCT_UNKNOWN 0x00
  72. #define MTIP_PRODUCT_ASICFPGA 0x11
  73. /* Device instance number, incremented each time a device is probed. */
  74. static int instance;
  75. /*
  76. * Global variable used to hold the major block device number
  77. * allocated in mtip_init().
  78. */
  79. static int mtip_major;
  80. static struct dentry *dfs_parent;
  81. static DEFINE_SPINLOCK(rssd_index_lock);
  82. static DEFINE_IDA(rssd_index_ida);
  83. static int mtip_block_initialize(struct driver_data *dd);
  84. #ifdef CONFIG_COMPAT
  85. struct mtip_compat_ide_task_request_s {
  86. __u8 io_ports[8];
  87. __u8 hob_ports[8];
  88. ide_reg_valid_t out_flags;
  89. ide_reg_valid_t in_flags;
  90. int data_phase;
  91. int req_cmd;
  92. compat_ulong_t out_size;
  93. compat_ulong_t in_size;
  94. };
  95. #endif
  96. /*
  97. * This function check_for_surprise_removal is called
  98. * while card is removed from the system and it will
  99. * read the vendor id from the configration space
  100. *
  101. * @pdev Pointer to the pci_dev structure.
  102. *
  103. * return value
  104. * true if device removed, else false
  105. */
  106. static bool mtip_check_surprise_removal(struct pci_dev *pdev)
  107. {
  108. u16 vendor_id = 0;
  109. /* Read the vendorID from the configuration space */
  110. pci_read_config_word(pdev, 0x00, &vendor_id);
  111. if (vendor_id == 0xFFFF)
  112. return true; /* device removed */
  113. return false; /* device present */
  114. }
  115. /*
  116. * This function is called for clean the pending command in the
  117. * command slot during the surprise removal of device and return
  118. * error to the upper layer.
  119. *
  120. * @dd Pointer to the DRIVER_DATA structure.
  121. *
  122. * return value
  123. * None
  124. */
  125. static void mtip_command_cleanup(struct driver_data *dd)
  126. {
  127. int group = 0, commandslot = 0, commandindex = 0;
  128. struct mtip_cmd *command;
  129. struct mtip_port *port = dd->port;
  130. static int in_progress;
  131. if (in_progress)
  132. return;
  133. in_progress = 1;
  134. for (group = 0; group < 4; group++) {
  135. for (commandslot = 0; commandslot < 32; commandslot++) {
  136. if (!(port->allocated[group] & (1 << commandslot)))
  137. continue;
  138. commandindex = group << 5 | commandslot;
  139. command = &port->commands[commandindex];
  140. if (atomic_read(&command->active)
  141. && (command->async_callback)) {
  142. command->async_callback(command->async_data,
  143. -ENODEV);
  144. command->async_callback = NULL;
  145. command->async_data = NULL;
  146. }
  147. dma_unmap_sg(&port->dd->pdev->dev,
  148. command->sg,
  149. command->scatter_ents,
  150. command->direction);
  151. }
  152. }
  153. up(&port->cmd_slot);
  154. set_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag);
  155. in_progress = 0;
  156. }
  157. /*
  158. * Obtain an empty command slot.
  159. *
  160. * This function needs to be reentrant since it could be called
  161. * at the same time on multiple CPUs. The allocation of the
  162. * command slot must be atomic.
  163. *
  164. * @port Pointer to the port data structure.
  165. *
  166. * return value
  167. * >= 0 Index of command slot obtained.
  168. * -1 No command slots available.
  169. */
  170. static int get_slot(struct mtip_port *port)
  171. {
  172. int slot, i;
  173. unsigned int num_command_slots = port->dd->slot_groups * 32;
  174. /*
  175. * Try 10 times, because there is a small race here.
  176. * that's ok, because it's still cheaper than a lock.
  177. *
  178. * Race: Since this section is not protected by lock, same bit
  179. * could be chosen by different process contexts running in
  180. * different processor. So instead of costly lock, we are going
  181. * with loop.
  182. */
  183. for (i = 0; i < 10; i++) {
  184. slot = find_next_zero_bit(port->allocated,
  185. num_command_slots, 1);
  186. if ((slot < num_command_slots) &&
  187. (!test_and_set_bit(slot, port->allocated)))
  188. return slot;
  189. }
  190. dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
  191. if (mtip_check_surprise_removal(port->dd->pdev)) {
  192. /* Device not present, clean outstanding commands */
  193. mtip_command_cleanup(port->dd);
  194. }
  195. return -1;
  196. }
  197. /*
  198. * Release a command slot.
  199. *
  200. * @port Pointer to the port data structure.
  201. * @tag Tag of command to release
  202. *
  203. * return value
  204. * None
  205. */
  206. static inline void release_slot(struct mtip_port *port, int tag)
  207. {
  208. smp_mb__before_clear_bit();
  209. clear_bit(tag, port->allocated);
  210. smp_mb__after_clear_bit();
  211. }
  212. /*
  213. * Reset the HBA (without sleeping)
  214. *
  215. * Just like hba_reset, except does not call sleep, so can be
  216. * run from interrupt/tasklet context.
  217. *
  218. * @dd Pointer to the driver data structure.
  219. *
  220. * return value
  221. * 0 The reset was successful.
  222. * -1 The HBA Reset bit did not clear.
  223. */
  224. static int hba_reset_nosleep(struct driver_data *dd)
  225. {
  226. unsigned long timeout;
  227. /* Chip quirk: quiesce any chip function */
  228. mdelay(10);
  229. /* Set the reset bit */
  230. writel(HOST_RESET, dd->mmio + HOST_CTL);
  231. /* Flush */
  232. readl(dd->mmio + HOST_CTL);
  233. /*
  234. * Wait 10ms then spin for up to 1 second
  235. * waiting for reset acknowledgement
  236. */
  237. timeout = jiffies + msecs_to_jiffies(1000);
  238. mdelay(10);
  239. while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
  240. && time_before(jiffies, timeout))
  241. mdelay(1);
  242. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
  243. return -1;
  244. if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
  245. return -1;
  246. return 0;
  247. }
  248. /*
  249. * Issue a command to the hardware.
  250. *
  251. * Set the appropriate bit in the s_active and Command Issue hardware
  252. * registers, causing hardware command processing to begin.
  253. *
  254. * @port Pointer to the port structure.
  255. * @tag The tag of the command to be issued.
  256. *
  257. * return value
  258. * None
  259. */
  260. static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
  261. {
  262. atomic_set(&port->commands[tag].active, 1);
  263. spin_lock(&port->cmd_issue_lock);
  264. writel((1 << MTIP_TAG_BIT(tag)),
  265. port->s_active[MTIP_TAG_INDEX(tag)]);
  266. writel((1 << MTIP_TAG_BIT(tag)),
  267. port->cmd_issue[MTIP_TAG_INDEX(tag)]);
  268. spin_unlock(&port->cmd_issue_lock);
  269. /* Set the command's timeout value.*/
  270. port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
  271. MTIP_NCQ_COMMAND_TIMEOUT_MS);
  272. }
  273. /*
  274. * Enable/disable the reception of FIS
  275. *
  276. * @port Pointer to the port data structure
  277. * @enable 1 to enable, 0 to disable
  278. *
  279. * return value
  280. * Previous state: 1 enabled, 0 disabled
  281. */
  282. static int mtip_enable_fis(struct mtip_port *port, int enable)
  283. {
  284. u32 tmp;
  285. /* enable FIS reception */
  286. tmp = readl(port->mmio + PORT_CMD);
  287. if (enable)
  288. writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
  289. else
  290. writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
  291. /* Flush */
  292. readl(port->mmio + PORT_CMD);
  293. return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
  294. }
  295. /*
  296. * Enable/disable the DMA engine
  297. *
  298. * @port Pointer to the port data structure
  299. * @enable 1 to enable, 0 to disable
  300. *
  301. * return value
  302. * Previous state: 1 enabled, 0 disabled.
  303. */
  304. static int mtip_enable_engine(struct mtip_port *port, int enable)
  305. {
  306. u32 tmp;
  307. /* enable FIS reception */
  308. tmp = readl(port->mmio + PORT_CMD);
  309. if (enable)
  310. writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
  311. else
  312. writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
  313. readl(port->mmio + PORT_CMD);
  314. return (((tmp & PORT_CMD_START) == PORT_CMD_START));
  315. }
  316. /*
  317. * Enables the port DMA engine and FIS reception.
  318. *
  319. * return value
  320. * None
  321. */
  322. static inline void mtip_start_port(struct mtip_port *port)
  323. {
  324. /* Enable FIS reception */
  325. mtip_enable_fis(port, 1);
  326. /* Enable the DMA engine */
  327. mtip_enable_engine(port, 1);
  328. }
  329. /*
  330. * Deinitialize a port by disabling port interrupts, the DMA engine,
  331. * and FIS reception.
  332. *
  333. * @port Pointer to the port structure
  334. *
  335. * return value
  336. * None
  337. */
  338. static inline void mtip_deinit_port(struct mtip_port *port)
  339. {
  340. /* Disable interrupts on this port */
  341. writel(0, port->mmio + PORT_IRQ_MASK);
  342. /* Disable the DMA engine */
  343. mtip_enable_engine(port, 0);
  344. /* Disable FIS reception */
  345. mtip_enable_fis(port, 0);
  346. }
  347. /*
  348. * Initialize a port.
  349. *
  350. * This function deinitializes the port by calling mtip_deinit_port() and
  351. * then initializes it by setting the command header and RX FIS addresses,
  352. * clearing the SError register and any pending port interrupts before
  353. * re-enabling the default set of port interrupts.
  354. *
  355. * @port Pointer to the port structure.
  356. *
  357. * return value
  358. * None
  359. */
  360. static void mtip_init_port(struct mtip_port *port)
  361. {
  362. int i;
  363. mtip_deinit_port(port);
  364. /* Program the command list base and FIS base addresses */
  365. if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
  366. writel((port->command_list_dma >> 16) >> 16,
  367. port->mmio + PORT_LST_ADDR_HI);
  368. writel((port->rxfis_dma >> 16) >> 16,
  369. port->mmio + PORT_FIS_ADDR_HI);
  370. }
  371. writel(port->command_list_dma & 0xFFFFFFFF,
  372. port->mmio + PORT_LST_ADDR);
  373. writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
  374. /* Clear SError */
  375. writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
  376. /* reset the completed registers.*/
  377. for (i = 0; i < port->dd->slot_groups; i++)
  378. writel(0xFFFFFFFF, port->completed[i]);
  379. /* Clear any pending interrupts for this port */
  380. writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
  381. /* Clear any pending interrupts on the HBA. */
  382. writel(readl(port->dd->mmio + HOST_IRQ_STAT),
  383. port->dd->mmio + HOST_IRQ_STAT);
  384. /* Enable port interrupts */
  385. writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
  386. }
  387. /*
  388. * Restart a port
  389. *
  390. * @port Pointer to the port data structure.
  391. *
  392. * return value
  393. * None
  394. */
  395. static void mtip_restart_port(struct mtip_port *port)
  396. {
  397. unsigned long timeout;
  398. /* Disable the DMA engine */
  399. mtip_enable_engine(port, 0);
  400. /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
  401. timeout = jiffies + msecs_to_jiffies(500);
  402. while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
  403. && time_before(jiffies, timeout))
  404. ;
  405. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  406. return;
  407. /*
  408. * Chip quirk: escalate to hba reset if
  409. * PxCMD.CR not clear after 500 ms
  410. */
  411. if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
  412. dev_warn(&port->dd->pdev->dev,
  413. "PxCMD.CR not clear, escalating reset\n");
  414. if (hba_reset_nosleep(port->dd))
  415. dev_err(&port->dd->pdev->dev,
  416. "HBA reset escalation failed.\n");
  417. /* 30 ms delay before com reset to quiesce chip */
  418. mdelay(30);
  419. }
  420. dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
  421. /* Set PxSCTL.DET */
  422. writel(readl(port->mmio + PORT_SCR_CTL) |
  423. 1, port->mmio + PORT_SCR_CTL);
  424. readl(port->mmio + PORT_SCR_CTL);
  425. /* Wait 1 ms to quiesce chip function */
  426. timeout = jiffies + msecs_to_jiffies(1);
  427. while (time_before(jiffies, timeout))
  428. ;
  429. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  430. return;
  431. /* Clear PxSCTL.DET */
  432. writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
  433. port->mmio + PORT_SCR_CTL);
  434. readl(port->mmio + PORT_SCR_CTL);
  435. /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
  436. timeout = jiffies + msecs_to_jiffies(500);
  437. while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
  438. && time_before(jiffies, timeout))
  439. ;
  440. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  441. return;
  442. if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
  443. dev_warn(&port->dd->pdev->dev,
  444. "COM reset failed\n");
  445. mtip_init_port(port);
  446. mtip_start_port(port);
  447. }
  448. /*
  449. * Helper function for tag logging
  450. */
  451. static void print_tags(struct driver_data *dd,
  452. char *msg,
  453. unsigned long *tagbits,
  454. int cnt)
  455. {
  456. unsigned char tagmap[128];
  457. int group, tagmap_len = 0;
  458. memset(tagmap, 0, sizeof(tagmap));
  459. for (group = SLOTBITS_IN_LONGS; group > 0; group--)
  460. tagmap_len = sprintf(tagmap + tagmap_len, "%016lX ",
  461. tagbits[group-1]);
  462. dev_warn(&dd->pdev->dev,
  463. "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
  464. }
  465. /*
  466. * Called periodically to see if any read/write commands are
  467. * taking too long to complete.
  468. *
  469. * @data Pointer to the PORT data structure.
  470. *
  471. * return value
  472. * None
  473. */
  474. static void mtip_timeout_function(unsigned long int data)
  475. {
  476. struct mtip_port *port = (struct mtip_port *) data;
  477. struct host_to_dev_fis *fis;
  478. struct mtip_cmd *command;
  479. int tag, cmdto_cnt = 0;
  480. unsigned int bit, group;
  481. unsigned int num_command_slots = port->dd->slot_groups * 32;
  482. unsigned long to, tagaccum[SLOTBITS_IN_LONGS];
  483. if (unlikely(!port))
  484. return;
  485. if (test_bit(MTIP_DDF_RESUME_BIT, &port->dd->dd_flag)) {
  486. mod_timer(&port->cmd_timer,
  487. jiffies + msecs_to_jiffies(30000));
  488. return;
  489. }
  490. /* clear the tag accumulator */
  491. memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
  492. for (tag = 0; tag < num_command_slots; tag++) {
  493. /*
  494. * Skip internal command slot as it has
  495. * its own timeout mechanism
  496. */
  497. if (tag == MTIP_TAG_INTERNAL)
  498. continue;
  499. if (atomic_read(&port->commands[tag].active) &&
  500. (time_after(jiffies, port->commands[tag].comp_time))) {
  501. group = tag >> 5;
  502. bit = tag & 0x1F;
  503. command = &port->commands[tag];
  504. fis = (struct host_to_dev_fis *) command->command;
  505. set_bit(tag, tagaccum);
  506. cmdto_cnt++;
  507. if (cmdto_cnt == 1)
  508. set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
  509. /*
  510. * Clear the completed bit. This should prevent
  511. * any interrupt handlers from trying to retire
  512. * the command.
  513. */
  514. writel(1 << bit, port->completed[group]);
  515. /* Call the async completion callback. */
  516. if (likely(command->async_callback))
  517. command->async_callback(command->async_data,
  518. -EIO);
  519. command->async_callback = NULL;
  520. command->comp_func = NULL;
  521. /* Unmap the DMA scatter list entries */
  522. dma_unmap_sg(&port->dd->pdev->dev,
  523. command->sg,
  524. command->scatter_ents,
  525. command->direction);
  526. /*
  527. * Clear the allocated bit and active tag for the
  528. * command.
  529. */
  530. atomic_set(&port->commands[tag].active, 0);
  531. release_slot(port, tag);
  532. up(&port->cmd_slot);
  533. }
  534. }
  535. if (cmdto_cnt && !test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
  536. print_tags(port->dd, "timed out", tagaccum, cmdto_cnt);
  537. mtip_restart_port(port);
  538. clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
  539. wake_up_interruptible(&port->svc_wait);
  540. }
  541. if (port->ic_pause_timer) {
  542. to = port->ic_pause_timer + msecs_to_jiffies(1000);
  543. if (time_after(jiffies, to)) {
  544. if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
  545. port->ic_pause_timer = 0;
  546. clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
  547. clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
  548. clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
  549. wake_up_interruptible(&port->svc_wait);
  550. }
  551. }
  552. }
  553. /* Restart the timer */
  554. mod_timer(&port->cmd_timer,
  555. jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
  556. }
  557. /*
  558. * IO completion function.
  559. *
  560. * This completion function is called by the driver ISR when a
  561. * command that was issued by the kernel completes. It first calls the
  562. * asynchronous completion function which normally calls back into the block
  563. * layer passing the asynchronous callback data, then unmaps the
  564. * scatter list associated with the completed command, and finally
  565. * clears the allocated bit associated with the completed command.
  566. *
  567. * @port Pointer to the port data structure.
  568. * @tag Tag of the command.
  569. * @data Pointer to driver_data.
  570. * @status Completion status.
  571. *
  572. * return value
  573. * None
  574. */
  575. static void mtip_async_complete(struct mtip_port *port,
  576. int tag,
  577. void *data,
  578. int status)
  579. {
  580. struct mtip_cmd *command;
  581. struct driver_data *dd = data;
  582. int cb_status = status ? -EIO : 0;
  583. if (unlikely(!dd) || unlikely(!port))
  584. return;
  585. command = &port->commands[tag];
  586. if (unlikely(status == PORT_IRQ_TF_ERR)) {
  587. dev_warn(&port->dd->pdev->dev,
  588. "Command tag %d failed due to TFE\n", tag);
  589. }
  590. /* Upper layer callback */
  591. if (likely(command->async_callback))
  592. command->async_callback(command->async_data, cb_status);
  593. command->async_callback = NULL;
  594. command->comp_func = NULL;
  595. /* Unmap the DMA scatter list entries */
  596. dma_unmap_sg(&dd->pdev->dev,
  597. command->sg,
  598. command->scatter_ents,
  599. command->direction);
  600. /* Clear the allocated and active bits for the command */
  601. atomic_set(&port->commands[tag].active, 0);
  602. release_slot(port, tag);
  603. up(&port->cmd_slot);
  604. }
  605. /*
  606. * Internal command completion callback function.
  607. *
  608. * This function is normally called by the driver ISR when an internal
  609. * command completed. This function signals the command completion by
  610. * calling complete().
  611. *
  612. * @port Pointer to the port data structure.
  613. * @tag Tag of the command that has completed.
  614. * @data Pointer to a completion structure.
  615. * @status Completion status.
  616. *
  617. * return value
  618. * None
  619. */
  620. static void mtip_completion(struct mtip_port *port,
  621. int tag,
  622. void *data,
  623. int status)
  624. {
  625. struct mtip_cmd *command = &port->commands[tag];
  626. struct completion *waiting = data;
  627. if (unlikely(status == PORT_IRQ_TF_ERR))
  628. dev_warn(&port->dd->pdev->dev,
  629. "Internal command %d completed with TFE\n", tag);
  630. command->async_callback = NULL;
  631. command->comp_func = NULL;
  632. complete(waiting);
  633. }
  634. static void mtip_null_completion(struct mtip_port *port,
  635. int tag,
  636. void *data,
  637. int status)
  638. {
  639. return;
  640. }
  641. static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
  642. dma_addr_t buffer_dma, unsigned int sectors);
  643. static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
  644. struct smart_attr *attrib);
  645. /*
  646. * Handle an error.
  647. *
  648. * @dd Pointer to the DRIVER_DATA structure.
  649. *
  650. * return value
  651. * None
  652. */
  653. static void mtip_handle_tfe(struct driver_data *dd)
  654. {
  655. int group, tag, bit, reissue, rv;
  656. struct mtip_port *port;
  657. struct mtip_cmd *cmd;
  658. u32 completed;
  659. struct host_to_dev_fis *fis;
  660. unsigned long tagaccum[SLOTBITS_IN_LONGS];
  661. unsigned int cmd_cnt = 0;
  662. unsigned char *buf;
  663. char *fail_reason = NULL;
  664. int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
  665. dev_warn(&dd->pdev->dev, "Taskfile error\n");
  666. port = dd->port;
  667. /* Stop the timer to prevent command timeouts. */
  668. del_timer(&port->cmd_timer);
  669. set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
  670. if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
  671. test_bit(MTIP_TAG_INTERNAL, port->allocated)) {
  672. cmd = &port->commands[MTIP_TAG_INTERNAL];
  673. dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
  674. atomic_inc(&cmd->active); /* active > 1 indicates error */
  675. if (cmd->comp_data && cmd->comp_func) {
  676. cmd->comp_func(port, MTIP_TAG_INTERNAL,
  677. cmd->comp_data, PORT_IRQ_TF_ERR);
  678. }
  679. goto handle_tfe_exit;
  680. }
  681. /* clear the tag accumulator */
  682. memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
  683. /* Loop through all the groups */
  684. for (group = 0; group < dd->slot_groups; group++) {
  685. completed = readl(port->completed[group]);
  686. /* clear completed status register in the hardware.*/
  687. writel(completed, port->completed[group]);
  688. /* Process successfully completed commands */
  689. for (bit = 0; bit < 32 && completed; bit++) {
  690. if (!(completed & (1<<bit)))
  691. continue;
  692. tag = (group << 5) + bit;
  693. /* Skip the internal command slot */
  694. if (tag == MTIP_TAG_INTERNAL)
  695. continue;
  696. cmd = &port->commands[tag];
  697. if (likely(cmd->comp_func)) {
  698. set_bit(tag, tagaccum);
  699. cmd_cnt++;
  700. atomic_set(&cmd->active, 0);
  701. cmd->comp_func(port,
  702. tag,
  703. cmd->comp_data,
  704. 0);
  705. } else {
  706. dev_err(&port->dd->pdev->dev,
  707. "Missing completion func for tag %d",
  708. tag);
  709. if (mtip_check_surprise_removal(dd->pdev)) {
  710. mtip_command_cleanup(dd);
  711. /* don't proceed further */
  712. return;
  713. }
  714. }
  715. }
  716. }
  717. print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
  718. /* Restart the port */
  719. mdelay(20);
  720. mtip_restart_port(port);
  721. /* Trying to determine the cause of the error */
  722. rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
  723. dd->port->log_buf,
  724. dd->port->log_buf_dma, 1);
  725. if (rv) {
  726. dev_warn(&dd->pdev->dev,
  727. "Error in READ LOG EXT (10h) command\n");
  728. /* non-critical error, don't fail the load */
  729. } else {
  730. buf = (unsigned char *)dd->port->log_buf;
  731. if (buf[259] & 0x1) {
  732. dev_info(&dd->pdev->dev,
  733. "Write protect bit is set.\n");
  734. set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
  735. fail_all_ncq_write = 1;
  736. fail_reason = "write protect";
  737. }
  738. if (buf[288] == 0xF7) {
  739. dev_info(&dd->pdev->dev,
  740. "Exceeded Tmax, drive in thermal shutdown.\n");
  741. set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
  742. fail_all_ncq_cmds = 1;
  743. fail_reason = "thermal shutdown";
  744. }
  745. if (buf[288] == 0xBF) {
  746. dev_info(&dd->pdev->dev,
  747. "Drive indicates rebuild has failed.\n");
  748. fail_all_ncq_cmds = 1;
  749. fail_reason = "rebuild failed";
  750. }
  751. }
  752. /* clear the tag accumulator */
  753. memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
  754. /* Loop through all the groups */
  755. for (group = 0; group < dd->slot_groups; group++) {
  756. for (bit = 0; bit < 32; bit++) {
  757. reissue = 1;
  758. tag = (group << 5) + bit;
  759. cmd = &port->commands[tag];
  760. /* If the active bit is set re-issue the command */
  761. if (atomic_read(&cmd->active) == 0)
  762. continue;
  763. fis = (struct host_to_dev_fis *)cmd->command;
  764. /* Should re-issue? */
  765. if (tag == MTIP_TAG_INTERNAL ||
  766. fis->command == ATA_CMD_SET_FEATURES)
  767. reissue = 0;
  768. else {
  769. if (fail_all_ncq_cmds ||
  770. (fail_all_ncq_write &&
  771. fis->command == ATA_CMD_FPDMA_WRITE)) {
  772. dev_warn(&dd->pdev->dev,
  773. " Fail: %s w/tag %d [%s].\n",
  774. fis->command == ATA_CMD_FPDMA_WRITE ?
  775. "write" : "read",
  776. tag,
  777. fail_reason != NULL ?
  778. fail_reason : "unknown");
  779. atomic_set(&cmd->active, 0);
  780. if (cmd->comp_func) {
  781. cmd->comp_func(port, tag,
  782. cmd->comp_data,
  783. -ENODATA);
  784. }
  785. continue;
  786. }
  787. }
  788. /*
  789. * First check if this command has
  790. * exceeded its retries.
  791. */
  792. if (reissue && (cmd->retries-- > 0)) {
  793. set_bit(tag, tagaccum);
  794. /* Re-issue the command. */
  795. mtip_issue_ncq_command(port, tag);
  796. continue;
  797. }
  798. /* Retire a command that will not be reissued */
  799. dev_warn(&port->dd->pdev->dev,
  800. "retiring tag %d\n", tag);
  801. atomic_set(&cmd->active, 0);
  802. if (cmd->comp_func)
  803. cmd->comp_func(
  804. port,
  805. tag,
  806. cmd->comp_data,
  807. PORT_IRQ_TF_ERR);
  808. else
  809. dev_warn(&port->dd->pdev->dev,
  810. "Bad completion for tag %d\n",
  811. tag);
  812. }
  813. }
  814. print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
  815. handle_tfe_exit:
  816. /* clear eh_active */
  817. clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
  818. wake_up_interruptible(&port->svc_wait);
  819. mod_timer(&port->cmd_timer,
  820. jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
  821. }
  822. /*
  823. * Handle a set device bits interrupt
  824. */
  825. static inline void mtip_process_sdbf(struct driver_data *dd)
  826. {
  827. struct mtip_port *port = dd->port;
  828. int group, tag, bit;
  829. u32 completed;
  830. struct mtip_cmd *command;
  831. /* walk all bits in all slot groups */
  832. for (group = 0; group < dd->slot_groups; group++) {
  833. completed = readl(port->completed[group]);
  834. if (!completed)
  835. continue;
  836. /* clear completed status register in the hardware.*/
  837. writel(completed, port->completed[group]);
  838. /* Process completed commands. */
  839. for (bit = 0;
  840. (bit < 32) && completed;
  841. bit++, completed >>= 1) {
  842. if (completed & 0x01) {
  843. tag = (group << 5) | bit;
  844. /* skip internal command slot. */
  845. if (unlikely(tag == MTIP_TAG_INTERNAL))
  846. continue;
  847. command = &port->commands[tag];
  848. /* make internal callback */
  849. if (likely(command->comp_func)) {
  850. command->comp_func(
  851. port,
  852. tag,
  853. command->comp_data,
  854. 0);
  855. } else {
  856. dev_warn(&dd->pdev->dev,
  857. "Null completion "
  858. "for tag %d",
  859. tag);
  860. if (mtip_check_surprise_removal(
  861. dd->pdev)) {
  862. mtip_command_cleanup(dd);
  863. return;
  864. }
  865. }
  866. }
  867. }
  868. }
  869. }
  870. /*
  871. * Process legacy pio and d2h interrupts
  872. */
  873. static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
  874. {
  875. struct mtip_port *port = dd->port;
  876. struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
  877. if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
  878. (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  879. & (1 << MTIP_TAG_INTERNAL))) {
  880. if (cmd->comp_func) {
  881. cmd->comp_func(port,
  882. MTIP_TAG_INTERNAL,
  883. cmd->comp_data,
  884. 0);
  885. return;
  886. }
  887. }
  888. return;
  889. }
  890. /*
  891. * Demux and handle errors
  892. */
  893. static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
  894. {
  895. if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
  896. mtip_handle_tfe(dd);
  897. if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
  898. dev_warn(&dd->pdev->dev,
  899. "Clearing PxSERR.DIAG.x\n");
  900. writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
  901. }
  902. if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
  903. dev_warn(&dd->pdev->dev,
  904. "Clearing PxSERR.DIAG.n\n");
  905. writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
  906. }
  907. if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
  908. dev_warn(&dd->pdev->dev,
  909. "Port stat errors %x unhandled\n",
  910. (port_stat & ~PORT_IRQ_HANDLED));
  911. }
  912. }
  913. static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
  914. {
  915. struct driver_data *dd = (struct driver_data *) data;
  916. struct mtip_port *port = dd->port;
  917. u32 hba_stat, port_stat;
  918. int rv = IRQ_NONE;
  919. hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
  920. if (hba_stat) {
  921. rv = IRQ_HANDLED;
  922. /* Acknowledge the interrupt status on the port.*/
  923. port_stat = readl(port->mmio + PORT_IRQ_STAT);
  924. writel(port_stat, port->mmio + PORT_IRQ_STAT);
  925. /* Demux port status */
  926. if (likely(port_stat & PORT_IRQ_SDB_FIS))
  927. mtip_process_sdbf(dd);
  928. if (unlikely(port_stat & PORT_IRQ_ERR)) {
  929. if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
  930. mtip_command_cleanup(dd);
  931. /* don't proceed further */
  932. return IRQ_HANDLED;
  933. }
  934. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  935. &dd->dd_flag))
  936. return rv;
  937. mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
  938. }
  939. if (unlikely(port_stat & PORT_IRQ_LEGACY))
  940. mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
  941. }
  942. /* acknowledge interrupt */
  943. writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
  944. return rv;
  945. }
  946. /*
  947. * Wrapper for mtip_handle_irq
  948. * (ignores return code)
  949. */
  950. static void mtip_tasklet(unsigned long data)
  951. {
  952. mtip_handle_irq((struct driver_data *) data);
  953. }
  954. /*
  955. * HBA interrupt subroutine.
  956. *
  957. * @irq IRQ number.
  958. * @instance Pointer to the driver data structure.
  959. *
  960. * return value
  961. * IRQ_HANDLED A HBA interrupt was pending and handled.
  962. * IRQ_NONE This interrupt was not for the HBA.
  963. */
  964. static irqreturn_t mtip_irq_handler(int irq, void *instance)
  965. {
  966. struct driver_data *dd = instance;
  967. tasklet_schedule(&dd->tasklet);
  968. return IRQ_HANDLED;
  969. }
  970. static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
  971. {
  972. atomic_set(&port->commands[tag].active, 1);
  973. writel(1 << MTIP_TAG_BIT(tag),
  974. port->cmd_issue[MTIP_TAG_INDEX(tag)]);
  975. }
  976. static bool mtip_pause_ncq(struct mtip_port *port,
  977. struct host_to_dev_fis *fis)
  978. {
  979. struct host_to_dev_fis *reply;
  980. unsigned long task_file_data;
  981. reply = port->rxfis + RX_FIS_D2H_REG;
  982. task_file_data = readl(port->mmio+PORT_TFDATA);
  983. if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
  984. clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
  985. if ((task_file_data & 1))
  986. return false;
  987. if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
  988. set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
  989. set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
  990. port->ic_pause_timer = jiffies;
  991. return true;
  992. } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
  993. (fis->features == 0x03)) {
  994. set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
  995. port->ic_pause_timer = jiffies;
  996. return true;
  997. } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
  998. ((fis->command == 0xFC) &&
  999. (fis->features == 0x27 || fis->features == 0x72 ||
  1000. fis->features == 0x62 || fis->features == 0x26))) {
  1001. /* Com reset after secure erase or lowlevel format */
  1002. mtip_restart_port(port);
  1003. return false;
  1004. }
  1005. return false;
  1006. }
  1007. /*
  1008. * Wait for port to quiesce
  1009. *
  1010. * @port Pointer to port data structure
  1011. * @timeout Max duration to wait (ms)
  1012. *
  1013. * return value
  1014. * 0 Success
  1015. * -EBUSY Commands still active
  1016. */
  1017. static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
  1018. {
  1019. unsigned long to;
  1020. unsigned int n;
  1021. unsigned int active = 1;
  1022. to = jiffies + msecs_to_jiffies(timeout);
  1023. do {
  1024. if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
  1025. test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
  1026. msleep(20);
  1027. continue; /* svc thd is actively issuing commands */
  1028. }
  1029. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  1030. return -EFAULT;
  1031. /*
  1032. * Ignore s_active bit 0 of array element 0.
  1033. * This bit will always be set
  1034. */
  1035. active = readl(port->s_active[0]) & 0xFFFFFFFE;
  1036. for (n = 1; n < port->dd->slot_groups; n++)
  1037. active |= readl(port->s_active[n]);
  1038. if (!active)
  1039. break;
  1040. msleep(20);
  1041. } while (time_before(jiffies, to));
  1042. return active ? -EBUSY : 0;
  1043. }
  1044. /*
  1045. * Execute an internal command and wait for the completion.
  1046. *
  1047. * @port Pointer to the port data structure.
  1048. * @fis Pointer to the FIS that describes the command.
  1049. * @fis_len Length in WORDS of the FIS.
  1050. * @buffer DMA accessible for command data.
  1051. * @buf_len Length, in bytes, of the data buffer.
  1052. * @opts Command header options, excluding the FIS length
  1053. * and the number of PRD entries.
  1054. * @timeout Time in ms to wait for the command to complete.
  1055. *
  1056. * return value
  1057. * 0 Command completed successfully.
  1058. * -EFAULT The buffer address is not correctly aligned.
  1059. * -EBUSY Internal command or other IO in progress.
  1060. * -EAGAIN Time out waiting for command to complete.
  1061. */
  1062. static int mtip_exec_internal_command(struct mtip_port *port,
  1063. struct host_to_dev_fis *fis,
  1064. int fis_len,
  1065. dma_addr_t buffer,
  1066. int buf_len,
  1067. u32 opts,
  1068. gfp_t atomic,
  1069. unsigned long timeout)
  1070. {
  1071. struct mtip_cmd_sg *command_sg;
  1072. DECLARE_COMPLETION_ONSTACK(wait);
  1073. int rv = 0, ready2go = 1;
  1074. struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
  1075. unsigned long to;
  1076. /* Make sure the buffer is 8 byte aligned. This is asic specific. */
  1077. if (buffer & 0x00000007) {
  1078. dev_err(&port->dd->pdev->dev,
  1079. "SG buffer is not 8 byte aligned\n");
  1080. return -EFAULT;
  1081. }
  1082. to = jiffies + msecs_to_jiffies(timeout);
  1083. do {
  1084. ready2go = !test_and_set_bit(MTIP_TAG_INTERNAL,
  1085. port->allocated);
  1086. if (ready2go)
  1087. break;
  1088. mdelay(100);
  1089. } while (time_before(jiffies, to));
  1090. if (!ready2go) {
  1091. dev_warn(&port->dd->pdev->dev,
  1092. "Internal cmd active. new cmd [%02X]\n", fis->command);
  1093. return -EBUSY;
  1094. }
  1095. set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
  1096. port->ic_pause_timer = 0;
  1097. if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
  1098. clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
  1099. else if (fis->command == ATA_CMD_DOWNLOAD_MICRO)
  1100. clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
  1101. if (atomic == GFP_KERNEL) {
  1102. if (fis->command != ATA_CMD_STANDBYNOW1) {
  1103. /* wait for io to complete if non atomic */
  1104. if (mtip_quiesce_io(port, 5000) < 0) {
  1105. dev_warn(&port->dd->pdev->dev,
  1106. "Failed to quiesce IO\n");
  1107. release_slot(port, MTIP_TAG_INTERNAL);
  1108. clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
  1109. wake_up_interruptible(&port->svc_wait);
  1110. return -EBUSY;
  1111. }
  1112. }
  1113. /* Set the completion function and data for the command. */
  1114. int_cmd->comp_data = &wait;
  1115. int_cmd->comp_func = mtip_completion;
  1116. } else {
  1117. /* Clear completion - we're going to poll */
  1118. int_cmd->comp_data = NULL;
  1119. int_cmd->comp_func = mtip_null_completion;
  1120. }
  1121. /* Copy the command to the command table */
  1122. memcpy(int_cmd->command, fis, fis_len*4);
  1123. /* Populate the SG list */
  1124. int_cmd->command_header->opts =
  1125. __force_bit2int cpu_to_le32(opts | fis_len);
  1126. if (buf_len) {
  1127. command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
  1128. command_sg->info =
  1129. __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
  1130. command_sg->dba =
  1131. __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
  1132. command_sg->dba_upper =
  1133. __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
  1134. int_cmd->command_header->opts |=
  1135. __force_bit2int cpu_to_le32((1 << 16));
  1136. }
  1137. /* Populate the command header */
  1138. int_cmd->command_header->byte_count = 0;
  1139. /* Issue the command to the hardware */
  1140. mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
  1141. /* Poll if atomic, wait_for_completion otherwise */
  1142. if (atomic == GFP_KERNEL) {
  1143. /* Wait for the command to complete or timeout. */
  1144. if (wait_for_completion_timeout(
  1145. &wait,
  1146. msecs_to_jiffies(timeout)) == 0) {
  1147. dev_err(&port->dd->pdev->dev,
  1148. "Internal command did not complete [%d] "
  1149. "within timeout of %lu ms\n",
  1150. atomic, timeout);
  1151. if (mtip_check_surprise_removal(port->dd->pdev) ||
  1152. test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  1153. &port->dd->dd_flag)) {
  1154. rv = -ENXIO;
  1155. goto exec_ic_exit;
  1156. }
  1157. rv = -EAGAIN;
  1158. }
  1159. } else {
  1160. /* Spin for <timeout> checking if command still outstanding */
  1161. timeout = jiffies + msecs_to_jiffies(timeout);
  1162. while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  1163. & (1 << MTIP_TAG_INTERNAL))
  1164. && time_before(jiffies, timeout)) {
  1165. if (mtip_check_surprise_removal(port->dd->pdev)) {
  1166. rv = -ENXIO;
  1167. goto exec_ic_exit;
  1168. }
  1169. if ((fis->command != ATA_CMD_STANDBYNOW1) &&
  1170. test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  1171. &port->dd->dd_flag)) {
  1172. rv = -ENXIO;
  1173. goto exec_ic_exit;
  1174. }
  1175. if (readl(port->mmio + PORT_IRQ_STAT) & PORT_IRQ_ERR) {
  1176. atomic_inc(&int_cmd->active); /* error */
  1177. break;
  1178. }
  1179. }
  1180. }
  1181. if (atomic_read(&int_cmd->active) > 1) {
  1182. dev_err(&port->dd->pdev->dev,
  1183. "Internal command [%02X] failed\n", fis->command);
  1184. rv = -EIO;
  1185. }
  1186. if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  1187. & (1 << MTIP_TAG_INTERNAL)) {
  1188. rv = -ENXIO;
  1189. if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  1190. &port->dd->dd_flag)) {
  1191. mtip_restart_port(port);
  1192. rv = -EAGAIN;
  1193. }
  1194. }
  1195. exec_ic_exit:
  1196. /* Clear the allocated and active bits for the internal command. */
  1197. atomic_set(&int_cmd->active, 0);
  1198. release_slot(port, MTIP_TAG_INTERNAL);
  1199. if (rv >= 0 && mtip_pause_ncq(port, fis)) {
  1200. /* NCQ paused */
  1201. return rv;
  1202. }
  1203. clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
  1204. wake_up_interruptible(&port->svc_wait);
  1205. return rv;
  1206. }
  1207. /*
  1208. * Byte-swap ATA ID strings.
  1209. *
  1210. * ATA identify data contains strings in byte-swapped 16-bit words.
  1211. * They must be swapped (on all architectures) to be usable as C strings.
  1212. * This function swaps bytes in-place.
  1213. *
  1214. * @buf The buffer location of the string
  1215. * @len The number of bytes to swap
  1216. *
  1217. * return value
  1218. * None
  1219. */
  1220. static inline void ata_swap_string(u16 *buf, unsigned int len)
  1221. {
  1222. int i;
  1223. for (i = 0; i < (len/2); i++)
  1224. be16_to_cpus(&buf[i]);
  1225. }
  1226. /*
  1227. * Request the device identity information.
  1228. *
  1229. * If a user space buffer is not specified, i.e. is NULL, the
  1230. * identify information is still read from the drive and placed
  1231. * into the identify data buffer (@e port->identify) in the
  1232. * port data structure.
  1233. * When the identify buffer contains valid identify information @e
  1234. * port->identify_valid is non-zero.
  1235. *
  1236. * @port Pointer to the port structure.
  1237. * @user_buffer A user space buffer where the identify data should be
  1238. * copied.
  1239. *
  1240. * return value
  1241. * 0 Command completed successfully.
  1242. * -EFAULT An error occurred while coping data to the user buffer.
  1243. * -1 Command failed.
  1244. */
  1245. static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
  1246. {
  1247. int rv = 0;
  1248. struct host_to_dev_fis fis;
  1249. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  1250. return -EFAULT;
  1251. /* Build the FIS. */
  1252. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1253. fis.type = 0x27;
  1254. fis.opts = 1 << 7;
  1255. fis.command = ATA_CMD_ID_ATA;
  1256. /* Set the identify information as invalid. */
  1257. port->identify_valid = 0;
  1258. /* Clear the identify information. */
  1259. memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
  1260. /* Execute the command. */
  1261. if (mtip_exec_internal_command(port,
  1262. &fis,
  1263. 5,
  1264. port->identify_dma,
  1265. sizeof(u16) * ATA_ID_WORDS,
  1266. 0,
  1267. GFP_KERNEL,
  1268. MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
  1269. < 0) {
  1270. rv = -1;
  1271. goto out;
  1272. }
  1273. /*
  1274. * Perform any necessary byte-swapping. Yes, the kernel does in fact
  1275. * perform field-sensitive swapping on the string fields.
  1276. * See the kernel use of ata_id_string() for proof of this.
  1277. */
  1278. #ifdef __LITTLE_ENDIAN
  1279. ata_swap_string(port->identify + 27, 40); /* model string*/
  1280. ata_swap_string(port->identify + 23, 8); /* firmware string*/
  1281. ata_swap_string(port->identify + 10, 20); /* serial# string*/
  1282. #else
  1283. {
  1284. int i;
  1285. for (i = 0; i < ATA_ID_WORDS; i++)
  1286. port->identify[i] = le16_to_cpu(port->identify[i]);
  1287. }
  1288. #endif
  1289. /* Set the identify buffer as valid. */
  1290. port->identify_valid = 1;
  1291. if (user_buffer) {
  1292. if (copy_to_user(
  1293. user_buffer,
  1294. port->identify,
  1295. ATA_ID_WORDS * sizeof(u16))) {
  1296. rv = -EFAULT;
  1297. goto out;
  1298. }
  1299. }
  1300. out:
  1301. return rv;
  1302. }
  1303. /*
  1304. * Issue a standby immediate command to the device.
  1305. *
  1306. * @port Pointer to the port structure.
  1307. *
  1308. * return value
  1309. * 0 Command was executed successfully.
  1310. * -1 An error occurred while executing the command.
  1311. */
  1312. static int mtip_standby_immediate(struct mtip_port *port)
  1313. {
  1314. int rv;
  1315. struct host_to_dev_fis fis;
  1316. unsigned long start;
  1317. /* Build the FIS. */
  1318. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1319. fis.type = 0x27;
  1320. fis.opts = 1 << 7;
  1321. fis.command = ATA_CMD_STANDBYNOW1;
  1322. start = jiffies;
  1323. rv = mtip_exec_internal_command(port,
  1324. &fis,
  1325. 5,
  1326. 0,
  1327. 0,
  1328. 0,
  1329. GFP_ATOMIC,
  1330. 15000);
  1331. dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
  1332. jiffies_to_msecs(jiffies - start));
  1333. if (rv)
  1334. dev_warn(&port->dd->pdev->dev,
  1335. "STANDBY IMMEDIATE command failed.\n");
  1336. return rv;
  1337. }
  1338. /*
  1339. * Issue a READ LOG EXT command to the device.
  1340. *
  1341. * @port pointer to the port structure.
  1342. * @page page number to fetch
  1343. * @buffer pointer to buffer
  1344. * @buffer_dma dma address corresponding to @buffer
  1345. * @sectors page length to fetch, in sectors
  1346. *
  1347. * return value
  1348. * @rv return value from mtip_exec_internal_command()
  1349. */
  1350. static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
  1351. dma_addr_t buffer_dma, unsigned int sectors)
  1352. {
  1353. struct host_to_dev_fis fis;
  1354. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1355. fis.type = 0x27;
  1356. fis.opts = 1 << 7;
  1357. fis.command = ATA_CMD_READ_LOG_EXT;
  1358. fis.sect_count = sectors & 0xFF;
  1359. fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
  1360. fis.lba_low = page;
  1361. fis.lba_mid = 0;
  1362. fis.device = ATA_DEVICE_OBS;
  1363. memset(buffer, 0, sectors * ATA_SECT_SIZE);
  1364. return mtip_exec_internal_command(port,
  1365. &fis,
  1366. 5,
  1367. buffer_dma,
  1368. sectors * ATA_SECT_SIZE,
  1369. 0,
  1370. GFP_ATOMIC,
  1371. MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
  1372. }
  1373. /*
  1374. * Issue a SMART READ DATA command to the device.
  1375. *
  1376. * @port pointer to the port structure.
  1377. * @buffer pointer to buffer
  1378. * @buffer_dma dma address corresponding to @buffer
  1379. *
  1380. * return value
  1381. * @rv return value from mtip_exec_internal_command()
  1382. */
  1383. static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
  1384. dma_addr_t buffer_dma)
  1385. {
  1386. struct host_to_dev_fis fis;
  1387. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1388. fis.type = 0x27;
  1389. fis.opts = 1 << 7;
  1390. fis.command = ATA_CMD_SMART;
  1391. fis.features = 0xD0;
  1392. fis.sect_count = 1;
  1393. fis.lba_mid = 0x4F;
  1394. fis.lba_hi = 0xC2;
  1395. fis.device = ATA_DEVICE_OBS;
  1396. return mtip_exec_internal_command(port,
  1397. &fis,
  1398. 5,
  1399. buffer_dma,
  1400. ATA_SECT_SIZE,
  1401. 0,
  1402. GFP_ATOMIC,
  1403. 15000);
  1404. }
  1405. /*
  1406. * Get the value of a smart attribute
  1407. *
  1408. * @port pointer to the port structure
  1409. * @id attribute number
  1410. * @attrib pointer to return attrib information corresponding to @id
  1411. *
  1412. * return value
  1413. * -EINVAL NULL buffer passed or unsupported attribute @id.
  1414. * -EPERM Identify data not valid, SMART not supported or not enabled
  1415. */
  1416. static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
  1417. struct smart_attr *attrib)
  1418. {
  1419. int rv, i;
  1420. struct smart_attr *pattr;
  1421. if (!attrib)
  1422. return -EINVAL;
  1423. if (!port->identify_valid) {
  1424. dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
  1425. return -EPERM;
  1426. }
  1427. if (!(port->identify[82] & 0x1)) {
  1428. dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
  1429. return -EPERM;
  1430. }
  1431. if (!(port->identify[85] & 0x1)) {
  1432. dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
  1433. return -EPERM;
  1434. }
  1435. memset(port->smart_buf, 0, ATA_SECT_SIZE);
  1436. rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
  1437. if (rv) {
  1438. dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
  1439. return rv;
  1440. }
  1441. pattr = (struct smart_attr *)(port->smart_buf + 2);
  1442. for (i = 0; i < 29; i++, pattr++)
  1443. if (pattr->attr_id == id) {
  1444. memcpy(attrib, pattr, sizeof(struct smart_attr));
  1445. break;
  1446. }
  1447. if (i == 29) {
  1448. dev_warn(&port->dd->pdev->dev,
  1449. "Query for invalid SMART attribute ID\n");
  1450. rv = -EINVAL;
  1451. }
  1452. return rv;
  1453. }
  1454. /*
  1455. * Get the drive capacity.
  1456. *
  1457. * @dd Pointer to the device data structure.
  1458. * @sectors Pointer to the variable that will receive the sector count.
  1459. *
  1460. * return value
  1461. * 1 Capacity was returned successfully.
  1462. * 0 The identify information is invalid.
  1463. */
  1464. static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
  1465. {
  1466. struct mtip_port *port = dd->port;
  1467. u64 total, raw0, raw1, raw2, raw3;
  1468. raw0 = port->identify[100];
  1469. raw1 = port->identify[101];
  1470. raw2 = port->identify[102];
  1471. raw3 = port->identify[103];
  1472. total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
  1473. *sectors = total;
  1474. return (bool) !!port->identify_valid;
  1475. }
  1476. /*
  1477. * Reset the HBA.
  1478. *
  1479. * Resets the HBA by setting the HBA Reset bit in the Global
  1480. * HBA Control register. After setting the HBA Reset bit the
  1481. * function waits for 1 second before reading the HBA Reset
  1482. * bit to make sure it has cleared. If HBA Reset is not clear
  1483. * an error is returned. Cannot be used in non-blockable
  1484. * context.
  1485. *
  1486. * @dd Pointer to the driver data structure.
  1487. *
  1488. * return value
  1489. * 0 The reset was successful.
  1490. * -1 The HBA Reset bit did not clear.
  1491. */
  1492. static int mtip_hba_reset(struct driver_data *dd)
  1493. {
  1494. mtip_deinit_port(dd->port);
  1495. /* Set the reset bit */
  1496. writel(HOST_RESET, dd->mmio + HOST_CTL);
  1497. /* Flush */
  1498. readl(dd->mmio + HOST_CTL);
  1499. /* Wait for reset to clear */
  1500. ssleep(1);
  1501. /* Check the bit has cleared */
  1502. if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
  1503. dev_err(&dd->pdev->dev,
  1504. "Reset bit did not clear.\n");
  1505. return -1;
  1506. }
  1507. return 0;
  1508. }
  1509. /*
  1510. * Display the identify command data.
  1511. *
  1512. * @port Pointer to the port data structure.
  1513. *
  1514. * return value
  1515. * None
  1516. */
  1517. static void mtip_dump_identify(struct mtip_port *port)
  1518. {
  1519. sector_t sectors;
  1520. unsigned short revid;
  1521. char cbuf[42];
  1522. if (!port->identify_valid)
  1523. return;
  1524. strlcpy(cbuf, (char *)(port->identify+10), 21);
  1525. dev_info(&port->dd->pdev->dev,
  1526. "Serial No.: %s\n", cbuf);
  1527. strlcpy(cbuf, (char *)(port->identify+23), 9);
  1528. dev_info(&port->dd->pdev->dev,
  1529. "Firmware Ver.: %s\n", cbuf);
  1530. strlcpy(cbuf, (char *)(port->identify+27), 41);
  1531. dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
  1532. if (mtip_hw_get_capacity(port->dd, &sectors))
  1533. dev_info(&port->dd->pdev->dev,
  1534. "Capacity: %llu sectors (%llu MB)\n",
  1535. (u64)sectors,
  1536. ((u64)sectors) * ATA_SECT_SIZE >> 20);
  1537. pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
  1538. switch (revid & 0xFF) {
  1539. case 0x1:
  1540. strlcpy(cbuf, "A0", 3);
  1541. break;
  1542. case 0x3:
  1543. strlcpy(cbuf, "A2", 3);
  1544. break;
  1545. default:
  1546. strlcpy(cbuf, "?", 2);
  1547. break;
  1548. }
  1549. dev_info(&port->dd->pdev->dev,
  1550. "Card Type: %s\n", cbuf);
  1551. }
  1552. /*
  1553. * Map the commands scatter list into the command table.
  1554. *
  1555. * @command Pointer to the command.
  1556. * @nents Number of scatter list entries.
  1557. *
  1558. * return value
  1559. * None
  1560. */
  1561. static inline void fill_command_sg(struct driver_data *dd,
  1562. struct mtip_cmd *command,
  1563. int nents)
  1564. {
  1565. int n;
  1566. unsigned int dma_len;
  1567. struct mtip_cmd_sg *command_sg;
  1568. struct scatterlist *sg = command->sg;
  1569. command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
  1570. for (n = 0; n < nents; n++) {
  1571. dma_len = sg_dma_len(sg);
  1572. if (dma_len > 0x400000)
  1573. dev_err(&dd->pdev->dev,
  1574. "DMA segment length truncated\n");
  1575. command_sg->info = __force_bit2int
  1576. cpu_to_le32((dma_len-1) & 0x3FFFFF);
  1577. command_sg->dba = __force_bit2int
  1578. cpu_to_le32(sg_dma_address(sg));
  1579. command_sg->dba_upper = __force_bit2int
  1580. cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
  1581. command_sg++;
  1582. sg++;
  1583. }
  1584. }
  1585. /*
  1586. * @brief Execute a drive command.
  1587. *
  1588. * return value 0 The command completed successfully.
  1589. * return value -1 An error occurred while executing the command.
  1590. */
  1591. static int exec_drive_task(struct mtip_port *port, u8 *command)
  1592. {
  1593. struct host_to_dev_fis fis;
  1594. struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
  1595. /* Build the FIS. */
  1596. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1597. fis.type = 0x27;
  1598. fis.opts = 1 << 7;
  1599. fis.command = command[0];
  1600. fis.features = command[1];
  1601. fis.sect_count = command[2];
  1602. fis.sector = command[3];
  1603. fis.cyl_low = command[4];
  1604. fis.cyl_hi = command[5];
  1605. fis.device = command[6] & ~0x10; /* Clear the dev bit*/
  1606. dbg_printk(MTIP_DRV_NAME " %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
  1607. __func__,
  1608. command[0],
  1609. command[1],
  1610. command[2],
  1611. command[3],
  1612. command[4],
  1613. command[5],
  1614. command[6]);
  1615. /* Execute the command. */
  1616. if (mtip_exec_internal_command(port,
  1617. &fis,
  1618. 5,
  1619. 0,
  1620. 0,
  1621. 0,
  1622. GFP_KERNEL,
  1623. MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
  1624. return -1;
  1625. }
  1626. command[0] = reply->command; /* Status*/
  1627. command[1] = reply->features; /* Error*/
  1628. command[4] = reply->cyl_low;
  1629. command[5] = reply->cyl_hi;
  1630. dbg_printk(MTIP_DRV_NAME " %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
  1631. __func__,
  1632. command[0],
  1633. command[1],
  1634. command[4],
  1635. command[5]);
  1636. return 0;
  1637. }
  1638. /*
  1639. * @brief Execute a drive command.
  1640. *
  1641. * @param port Pointer to the port data structure.
  1642. * @param command Pointer to the user specified command parameters.
  1643. * @param user_buffer Pointer to the user space buffer where read sector
  1644. * data should be copied.
  1645. *
  1646. * return value 0 The command completed successfully.
  1647. * return value -EFAULT An error occurred while copying the completion
  1648. * data to the user space buffer.
  1649. * return value -1 An error occurred while executing the command.
  1650. */
  1651. static int exec_drive_command(struct mtip_port *port, u8 *command,
  1652. void __user *user_buffer)
  1653. {
  1654. struct host_to_dev_fis fis;
  1655. struct host_to_dev_fis *reply;
  1656. u8 *buf = NULL;
  1657. dma_addr_t dma_addr = 0;
  1658. int rv = 0, xfer_sz = command[3];
  1659. if (xfer_sz) {
  1660. if (!user_buffer)
  1661. return -EFAULT;
  1662. buf = dmam_alloc_coherent(&port->dd->pdev->dev,
  1663. ATA_SECT_SIZE * xfer_sz,
  1664. &dma_addr,
  1665. GFP_KERNEL);
  1666. if (!buf) {
  1667. dev_err(&port->dd->pdev->dev,
  1668. "Memory allocation failed (%d bytes)\n",
  1669. ATA_SECT_SIZE * xfer_sz);
  1670. return -ENOMEM;
  1671. }
  1672. memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
  1673. }
  1674. /* Build the FIS. */
  1675. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1676. fis.type = 0x27;
  1677. fis.opts = 1 << 7;
  1678. fis.command = command[0];
  1679. fis.features = command[2];
  1680. fis.sect_count = command[3];
  1681. if (fis.command == ATA_CMD_SMART) {
  1682. fis.sector = command[1];
  1683. fis.cyl_low = 0x4F;
  1684. fis.cyl_hi = 0xC2;
  1685. }
  1686. if (xfer_sz)
  1687. reply = (port->rxfis + RX_FIS_PIO_SETUP);
  1688. else
  1689. reply = (port->rxfis + RX_FIS_D2H_REG);
  1690. dbg_printk(MTIP_DRV_NAME
  1691. " %s: User Command: cmd %x, sect %x, "
  1692. "feat %x, sectcnt %x\n",
  1693. __func__,
  1694. command[0],
  1695. command[1],
  1696. command[2],
  1697. command[3]);
  1698. /* Execute the command. */
  1699. if (mtip_exec_internal_command(port,
  1700. &fis,
  1701. 5,
  1702. (xfer_sz ? dma_addr : 0),
  1703. (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
  1704. 0,
  1705. GFP_KERNEL,
  1706. MTIP_IOCTL_COMMAND_TIMEOUT_MS)
  1707. < 0) {
  1708. rv = -EFAULT;
  1709. goto exit_drive_command;
  1710. }
  1711. /* Collect the completion status. */
  1712. command[0] = reply->command; /* Status*/
  1713. command[1] = reply->features; /* Error*/
  1714. command[2] = reply->sect_count;
  1715. dbg_printk(MTIP_DRV_NAME
  1716. " %s: Completion Status: stat %x, "
  1717. "err %x, nsect %x\n",
  1718. __func__,
  1719. command[0],
  1720. command[1],
  1721. command[2]);
  1722. if (xfer_sz) {
  1723. if (copy_to_user(user_buffer,
  1724. buf,
  1725. ATA_SECT_SIZE * command[3])) {
  1726. rv = -EFAULT;
  1727. goto exit_drive_command;
  1728. }
  1729. }
  1730. exit_drive_command:
  1731. if (buf)
  1732. dmam_free_coherent(&port->dd->pdev->dev,
  1733. ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
  1734. return rv;
  1735. }
  1736. /*
  1737. * Indicates whether a command has a single sector payload.
  1738. *
  1739. * @command passed to the device to perform the certain event.
  1740. * @features passed to the device to perform the certain event.
  1741. *
  1742. * return value
  1743. * 1 command is one that always has a single sector payload,
  1744. * regardless of the value in the Sector Count field.
  1745. * 0 otherwise
  1746. *
  1747. */
  1748. static unsigned int implicit_sector(unsigned char command,
  1749. unsigned char features)
  1750. {
  1751. unsigned int rv = 0;
  1752. /* list of commands that have an implicit sector count of 1 */
  1753. switch (command) {
  1754. case ATA_CMD_SEC_SET_PASS:
  1755. case ATA_CMD_SEC_UNLOCK:
  1756. case ATA_CMD_SEC_ERASE_PREP:
  1757. case ATA_CMD_SEC_ERASE_UNIT:
  1758. case ATA_CMD_SEC_FREEZE_LOCK:
  1759. case ATA_CMD_SEC_DISABLE_PASS:
  1760. case ATA_CMD_PMP_READ:
  1761. case ATA_CMD_PMP_WRITE:
  1762. rv = 1;
  1763. break;
  1764. case ATA_CMD_SET_MAX:
  1765. if (features == ATA_SET_MAX_UNLOCK)
  1766. rv = 1;
  1767. break;
  1768. case ATA_CMD_SMART:
  1769. if ((features == ATA_SMART_READ_VALUES) ||
  1770. (features == ATA_SMART_READ_THRESHOLDS))
  1771. rv = 1;
  1772. break;
  1773. case ATA_CMD_CONF_OVERLAY:
  1774. if ((features == ATA_DCO_IDENTIFY) ||
  1775. (features == ATA_DCO_SET))
  1776. rv = 1;
  1777. break;
  1778. }
  1779. return rv;
  1780. }
  1781. static void mtip_set_timeout(struct host_to_dev_fis *fis, unsigned int *timeout)
  1782. {
  1783. switch (fis->command) {
  1784. case ATA_CMD_DOWNLOAD_MICRO:
  1785. *timeout = 120000; /* 2 minutes */
  1786. break;
  1787. case ATA_CMD_SEC_ERASE_UNIT:
  1788. case 0xFC:
  1789. *timeout = 240000; /* 4 minutes */
  1790. break;
  1791. case ATA_CMD_STANDBYNOW1:
  1792. *timeout = 120000; /* 2 minutes */
  1793. break;
  1794. case 0xF7:
  1795. case 0xFA:
  1796. *timeout = 60000; /* 60 seconds */
  1797. break;
  1798. case ATA_CMD_SMART:
  1799. *timeout = 15000; /* 15 seconds */
  1800. break;
  1801. default:
  1802. *timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
  1803. break;
  1804. }
  1805. }
  1806. /*
  1807. * Executes a taskfile
  1808. * See ide_taskfile_ioctl() for derivation
  1809. */
  1810. static int exec_drive_taskfile(struct driver_data *dd,
  1811. void __user *buf,
  1812. ide_task_request_t *req_task,
  1813. int outtotal)
  1814. {
  1815. struct host_to_dev_fis fis;
  1816. struct host_to_dev_fis *reply;
  1817. u8 *outbuf = NULL;
  1818. u8 *inbuf = NULL;
  1819. dma_addr_t outbuf_dma = 0;
  1820. dma_addr_t inbuf_dma = 0;
  1821. dma_addr_t dma_buffer = 0;
  1822. int err = 0;
  1823. unsigned int taskin = 0;
  1824. unsigned int taskout = 0;
  1825. u8 nsect = 0;
  1826. unsigned int timeout;
  1827. unsigned int force_single_sector;
  1828. unsigned int transfer_size;
  1829. unsigned long task_file_data;
  1830. int intotal = outtotal + req_task->out_size;
  1831. taskout = req_task->out_size;
  1832. taskin = req_task->in_size;
  1833. /* 130560 = 512 * 0xFF*/
  1834. if (taskin > 130560 || taskout > 130560) {
  1835. err = -EINVAL;
  1836. goto abort;
  1837. }
  1838. if (taskout) {
  1839. outbuf = kzalloc(taskout, GFP_KERNEL);
  1840. if (outbuf == NULL) {
  1841. err = -ENOMEM;
  1842. goto abort;
  1843. }
  1844. if (copy_from_user(outbuf, buf + outtotal, taskout)) {
  1845. err = -EFAULT;
  1846. goto abort;
  1847. }
  1848. outbuf_dma = pci_map_single(dd->pdev,
  1849. outbuf,
  1850. taskout,
  1851. DMA_TO_DEVICE);
  1852. if (outbuf_dma == 0) {
  1853. err = -ENOMEM;
  1854. goto abort;
  1855. }
  1856. dma_buffer = outbuf_dma;
  1857. }
  1858. if (taskin) {
  1859. inbuf = kzalloc(taskin, GFP_KERNEL);
  1860. if (inbuf == NULL) {
  1861. err = -ENOMEM;
  1862. goto abort;
  1863. }
  1864. if (copy_from_user(inbuf, buf + intotal, taskin)) {
  1865. err = -EFAULT;
  1866. goto abort;
  1867. }
  1868. inbuf_dma = pci_map_single(dd->pdev,
  1869. inbuf,
  1870. taskin, DMA_FROM_DEVICE);
  1871. if (inbuf_dma == 0) {
  1872. err = -ENOMEM;
  1873. goto abort;
  1874. }
  1875. dma_buffer = inbuf_dma;
  1876. }
  1877. /* only supports PIO and non-data commands from this ioctl. */
  1878. switch (req_task->data_phase) {
  1879. case TASKFILE_OUT:
  1880. nsect = taskout / ATA_SECT_SIZE;
  1881. reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
  1882. break;
  1883. case TASKFILE_IN:
  1884. reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
  1885. break;
  1886. case TASKFILE_NO_DATA:
  1887. reply = (dd->port->rxfis + RX_FIS_D2H_REG);
  1888. break;
  1889. default:
  1890. err = -EINVAL;
  1891. goto abort;
  1892. }
  1893. /* Build the FIS. */
  1894. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1895. fis.type = 0x27;
  1896. fis.opts = 1 << 7;
  1897. fis.command = req_task->io_ports[7];
  1898. fis.features = req_task->io_ports[1];
  1899. fis.sect_count = req_task->io_ports[2];
  1900. fis.lba_low = req_task->io_ports[3];
  1901. fis.lba_mid = req_task->io_ports[4];
  1902. fis.lba_hi = req_task->io_ports[5];
  1903. /* Clear the dev bit*/
  1904. fis.device = req_task->io_ports[6] & ~0x10;
  1905. if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
  1906. req_task->in_flags.all =
  1907. IDE_TASKFILE_STD_IN_FLAGS |
  1908. (IDE_HOB_STD_IN_FLAGS << 8);
  1909. fis.lba_low_ex = req_task->hob_ports[3];
  1910. fis.lba_mid_ex = req_task->hob_ports[4];
  1911. fis.lba_hi_ex = req_task->hob_ports[5];
  1912. fis.features_ex = req_task->hob_ports[1];
  1913. fis.sect_cnt_ex = req_task->hob_ports[2];
  1914. } else {
  1915. req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
  1916. }
  1917. force_single_sector = implicit_sector(fis.command, fis.features);
  1918. if ((taskin || taskout) && (!fis.sect_count)) {
  1919. if (nsect)
  1920. fis.sect_count = nsect;
  1921. else {
  1922. if (!force_single_sector) {
  1923. dev_warn(&dd->pdev->dev,
  1924. "data movement but "
  1925. "sect_count is 0\n");
  1926. err = -EINVAL;
  1927. goto abort;
  1928. }
  1929. }
  1930. }
  1931. dbg_printk(MTIP_DRV_NAME
  1932. " %s: cmd %x, feat %x, nsect %x,"
  1933. " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
  1934. " head/dev %x\n",
  1935. __func__,
  1936. fis.command,
  1937. fis.features,
  1938. fis.sect_count,
  1939. fis.lba_low,
  1940. fis.lba_mid,
  1941. fis.lba_hi,
  1942. fis.device);
  1943. mtip_set_timeout(&fis, &timeout);
  1944. /* Determine the correct transfer size.*/
  1945. if (force_single_sector)
  1946. transfer_size = ATA_SECT_SIZE;
  1947. else
  1948. transfer_size = ATA_SECT_SIZE * fis.sect_count;
  1949. /* Execute the command.*/
  1950. if (mtip_exec_internal_command(dd->port,
  1951. &fis,
  1952. 5,
  1953. dma_buffer,
  1954. transfer_size,
  1955. 0,
  1956. GFP_KERNEL,
  1957. timeout) < 0) {
  1958. err = -EIO;
  1959. goto abort;
  1960. }
  1961. task_file_data = readl(dd->port->mmio+PORT_TFDATA);
  1962. if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
  1963. reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
  1964. req_task->io_ports[7] = reply->control;
  1965. } else {
  1966. reply = dd->port->rxfis + RX_FIS_D2H_REG;
  1967. req_task->io_ports[7] = reply->command;
  1968. }
  1969. /* reclaim the DMA buffers.*/
  1970. if (inbuf_dma)
  1971. pci_unmap_single(dd->pdev, inbuf_dma,
  1972. taskin, DMA_FROM_DEVICE);
  1973. if (outbuf_dma)
  1974. pci_unmap_single(dd->pdev, outbuf_dma,
  1975. taskout, DMA_TO_DEVICE);
  1976. inbuf_dma = 0;
  1977. outbuf_dma = 0;
  1978. /* return the ATA registers to the caller.*/
  1979. req_task->io_ports[1] = reply->features;
  1980. req_task->io_ports[2] = reply->sect_count;
  1981. req_task->io_ports[3] = reply->lba_low;
  1982. req_task->io_ports[4] = reply->lba_mid;
  1983. req_task->io_ports[5] = reply->lba_hi;
  1984. req_task->io_ports[6] = reply->device;
  1985. if (req_task->out_flags.all & 1) {
  1986. req_task->hob_ports[3] = reply->lba_low_ex;
  1987. req_task->hob_ports[4] = reply->lba_mid_ex;
  1988. req_task->hob_ports[5] = reply->lba_hi_ex;
  1989. req_task->hob_ports[1] = reply->features_ex;
  1990. req_task->hob_ports[2] = reply->sect_cnt_ex;
  1991. }
  1992. dbg_printk(MTIP_DRV_NAME
  1993. " %s: Completion: stat %x,"
  1994. "err %x, sect_cnt %x, lbalo %x,"
  1995. "lbamid %x, lbahi %x, dev %x\n",
  1996. __func__,
  1997. req_task->io_ports[7],
  1998. req_task->io_ports[1],
  1999. req_task->io_ports[2],
  2000. req_task->io_ports[3],
  2001. req_task->io_ports[4],
  2002. req_task->io_ports[5],
  2003. req_task->io_ports[6]);
  2004. if (taskout) {
  2005. if (copy_to_user(buf + outtotal, outbuf, taskout)) {
  2006. err = -EFAULT;
  2007. goto abort;
  2008. }
  2009. }
  2010. if (taskin) {
  2011. if (copy_to_user(buf + intotal, inbuf, taskin)) {
  2012. err = -EFAULT;
  2013. goto abort;
  2014. }
  2015. }
  2016. abort:
  2017. if (inbuf_dma)
  2018. pci_unmap_single(dd->pdev, inbuf_dma,
  2019. taskin, DMA_FROM_DEVICE);
  2020. if (outbuf_dma)
  2021. pci_unmap_single(dd->pdev, outbuf_dma,
  2022. taskout, DMA_TO_DEVICE);
  2023. kfree(outbuf);
  2024. kfree(inbuf);
  2025. return err;
  2026. }
  2027. /*
  2028. * Handle IOCTL calls from the Block Layer.
  2029. *
  2030. * This function is called by the Block Layer when it receives an IOCTL
  2031. * command that it does not understand. If the IOCTL command is not supported
  2032. * this function returns -ENOTTY.
  2033. *
  2034. * @dd Pointer to the driver data structure.
  2035. * @cmd IOCTL command passed from the Block Layer.
  2036. * @arg IOCTL argument passed from the Block Layer.
  2037. *
  2038. * return value
  2039. * 0 The IOCTL completed successfully.
  2040. * -ENOTTY The specified command is not supported.
  2041. * -EFAULT An error occurred copying data to a user space buffer.
  2042. * -EIO An error occurred while executing the command.
  2043. */
  2044. static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
  2045. unsigned long arg)
  2046. {
  2047. switch (cmd) {
  2048. case HDIO_GET_IDENTITY:
  2049. {
  2050. if (copy_to_user((void __user *)arg, dd->port->identify,
  2051. sizeof(u16) * ATA_ID_WORDS))
  2052. return -EFAULT;
  2053. break;
  2054. }
  2055. case HDIO_DRIVE_CMD:
  2056. {
  2057. u8 drive_command[4];
  2058. /* Copy the user command info to our buffer. */
  2059. if (copy_from_user(drive_command,
  2060. (void __user *) arg,
  2061. sizeof(drive_command)))
  2062. return -EFAULT;
  2063. /* Execute the drive command. */
  2064. if (exec_drive_command(dd->port,
  2065. drive_command,
  2066. (void __user *) (arg+4)))
  2067. return -EIO;
  2068. /* Copy the status back to the users buffer. */
  2069. if (copy_to_user((void __user *) arg,
  2070. drive_command,
  2071. sizeof(drive_command)))
  2072. return -EFAULT;
  2073. break;
  2074. }
  2075. case HDIO_DRIVE_TASK:
  2076. {
  2077. u8 drive_command[7];
  2078. /* Copy the user command info to our buffer. */
  2079. if (copy_from_user(drive_command,
  2080. (void __user *) arg,
  2081. sizeof(drive_command)))
  2082. return -EFAULT;
  2083. /* Execute the drive command. */
  2084. if (exec_drive_task(dd->port, drive_command))
  2085. return -EIO;
  2086. /* Copy the status back to the users buffer. */
  2087. if (copy_to_user((void __user *) arg,
  2088. drive_command,
  2089. sizeof(drive_command)))
  2090. return -EFAULT;
  2091. break;
  2092. }
  2093. case HDIO_DRIVE_TASKFILE: {
  2094. ide_task_request_t req_task;
  2095. int ret, outtotal;
  2096. if (copy_from_user(&req_task, (void __user *) arg,
  2097. sizeof(req_task)))
  2098. return -EFAULT;
  2099. outtotal = sizeof(req_task);
  2100. ret = exec_drive_taskfile(dd, (void __user *) arg,
  2101. &req_task, outtotal);
  2102. if (copy_to_user((void __user *) arg, &req_task,
  2103. sizeof(req_task)))
  2104. return -EFAULT;
  2105. return ret;
  2106. }
  2107. default:
  2108. return -EINVAL;
  2109. }
  2110. return 0;
  2111. }
  2112. /*
  2113. * Submit an IO to the hw
  2114. *
  2115. * This function is called by the block layer to issue an io
  2116. * to the device. Upon completion, the callback function will
  2117. * be called with the data parameter passed as the callback data.
  2118. *
  2119. * @dd Pointer to the driver data structure.
  2120. * @start First sector to read.
  2121. * @nsect Number of sectors to read.
  2122. * @nents Number of entries in scatter list for the read command.
  2123. * @tag The tag of this read command.
  2124. * @callback Pointer to the function that should be called
  2125. * when the read completes.
  2126. * @data Callback data passed to the callback function
  2127. * when the read completes.
  2128. * @dir Direction (read or write)
  2129. *
  2130. * return value
  2131. * None
  2132. */
  2133. static void mtip_hw_submit_io(struct driver_data *dd, sector_t start,
  2134. int nsect, int nents, int tag, void *callback,
  2135. void *data, int dir)
  2136. {
  2137. struct host_to_dev_fis *fis;
  2138. struct mtip_port *port = dd->port;
  2139. struct mtip_cmd *command = &port->commands[tag];
  2140. int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  2141. /* Map the scatter list for DMA access */
  2142. nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
  2143. command->scatter_ents = nents;
  2144. /*
  2145. * The number of retries for this command before it is
  2146. * reported as a failure to the upper layers.
  2147. */
  2148. command->retries = MTIP_MAX_RETRIES;
  2149. /* Fill out fis */
  2150. fis = command->command;
  2151. fis->type = 0x27;
  2152. fis->opts = 1 << 7;
  2153. fis->command =
  2154. (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
  2155. *((unsigned int *) &fis->lba_low) = (start & 0xFFFFFF);
  2156. *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xFFFFFF);
  2157. fis->device = 1 << 6;
  2158. fis->features = nsect & 0xFF;
  2159. fis->features_ex = (nsect >> 8) & 0xFF;
  2160. fis->sect_count = ((tag << 3) | (tag >> 5));
  2161. fis->sect_cnt_ex = 0;
  2162. fis->control = 0;
  2163. fis->res2 = 0;
  2164. fis->res3 = 0;
  2165. fill_command_sg(dd, command, nents);
  2166. /* Populate the command header */
  2167. command->command_header->opts =
  2168. __force_bit2int cpu_to_le32(
  2169. (nents << 16) | 5 | AHCI_CMD_PREFETCH);
  2170. command->command_header->byte_count = 0;
  2171. /*
  2172. * Set the completion function and data for the command
  2173. * within this layer.
  2174. */
  2175. command->comp_data = dd;
  2176. command->comp_func = mtip_async_complete;
  2177. command->direction = dma_dir;
  2178. /*
  2179. * Set the completion function and data for the command passed
  2180. * from the upper layer.
  2181. */
  2182. command->async_data = data;
  2183. command->async_callback = callback;
  2184. /*
  2185. * To prevent this command from being issued
  2186. * if an internal command is in progress or error handling is active.
  2187. */
  2188. if (port->flags & MTIP_PF_PAUSE_IO) {
  2189. set_bit(tag, port->cmds_to_issue);
  2190. set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
  2191. return;
  2192. }
  2193. /* Issue the command to the hardware */
  2194. mtip_issue_ncq_command(port, tag);
  2195. return;
  2196. }
  2197. /*
  2198. * Release a command slot.
  2199. *
  2200. * @dd Pointer to the driver data structure.
  2201. * @tag Slot tag
  2202. *
  2203. * return value
  2204. * None
  2205. */
  2206. static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
  2207. {
  2208. release_slot(dd->port, tag);
  2209. }
  2210. /*
  2211. * Obtain a command slot and return its associated scatter list.
  2212. *
  2213. * @dd Pointer to the driver data structure.
  2214. * @tag Pointer to an int that will receive the allocated command
  2215. * slot tag.
  2216. *
  2217. * return value
  2218. * Pointer to the scatter list for the allocated command slot
  2219. * or NULL if no command slots are available.
  2220. */
  2221. static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
  2222. int *tag)
  2223. {
  2224. /*
  2225. * It is possible that, even with this semaphore, a thread
  2226. * may think that no command slots are available. Therefore, we
  2227. * need to make an attempt to get_slot().
  2228. */
  2229. down(&dd->port->cmd_slot);
  2230. *tag = get_slot(dd->port);
  2231. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
  2232. up(&dd->port->cmd_slot);
  2233. return NULL;
  2234. }
  2235. if (unlikely(*tag < 0)) {
  2236. up(&dd->port->cmd_slot);
  2237. return NULL;
  2238. }
  2239. return dd->port->commands[*tag].sg;
  2240. }
  2241. /*
  2242. * Sysfs status dump.
  2243. *
  2244. * @dev Pointer to the device structure, passed by the kernrel.
  2245. * @attr Pointer to the device_attribute structure passed by the kernel.
  2246. * @buf Pointer to the char buffer that will receive the stats info.
  2247. *
  2248. * return value
  2249. * The size, in bytes, of the data copied into buf.
  2250. */
  2251. static ssize_t mtip_hw_show_status(struct device *dev,
  2252. struct device_attribute *attr,
  2253. char *buf)
  2254. {
  2255. struct driver_data *dd = dev_to_disk(dev)->private_data;
  2256. int size = 0;
  2257. if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
  2258. size += sprintf(buf, "%s", "thermal_shutdown\n");
  2259. else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
  2260. size += sprintf(buf, "%s", "write_protect\n");
  2261. else
  2262. size += sprintf(buf, "%s", "online\n");
  2263. return size;
  2264. }
  2265. static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
  2266. static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
  2267. size_t len, loff_t *offset)
  2268. {
  2269. struct driver_data *dd = (struct driver_data *)f->private_data;
  2270. char buf[MTIP_DFS_MAX_BUF_SIZE];
  2271. u32 group_allocated;
  2272. int size = *offset;
  2273. int n;
  2274. if (!len || size)
  2275. return 0;
  2276. size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
  2277. for (n = dd->slot_groups-1; n >= 0; n--)
  2278. size += sprintf(&buf[size], "%08X ",
  2279. readl(dd->port->s_active[n]));
  2280. size += sprintf(&buf[size], "]\n");
  2281. size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
  2282. for (n = dd->slot_groups-1; n >= 0; n--)
  2283. size += sprintf(&buf[size], "%08X ",
  2284. readl(dd->port->cmd_issue[n]));
  2285. size += sprintf(&buf[size], "]\n");
  2286. size += sprintf(&buf[size], "H/ Completed : [ 0x");
  2287. for (n = dd->slot_groups-1; n >= 0; n--)
  2288. size += sprintf(&buf[size], "%08X ",
  2289. readl(dd->port->completed[n]));
  2290. size += sprintf(&buf[size], "]\n");
  2291. size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
  2292. readl(dd->port->mmio + PORT_IRQ_STAT));
  2293. size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
  2294. readl(dd->mmio + HOST_IRQ_STAT));
  2295. size += sprintf(&buf[size], "\n");
  2296. size += sprintf(&buf[size], "L/ Allocated : [ 0x");
  2297. for (n = dd->slot_groups-1; n >= 0; n--) {
  2298. if (sizeof(long) > sizeof(u32))
  2299. group_allocated =
  2300. dd->port->allocated[n/2] >> (32*(n&1));
  2301. else
  2302. group_allocated = dd->port->allocated[n];
  2303. size += sprintf(&buf[size], "%08X ", group_allocated);
  2304. }
  2305. size += sprintf(&buf[size], "]\n");
  2306. size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
  2307. for (n = dd->slot_groups-1; n >= 0; n--) {
  2308. if (sizeof(long) > sizeof(u32))
  2309. group_allocated =
  2310. dd->port->cmds_to_issue[n/2] >> (32*(n&1));
  2311. else
  2312. group_allocated = dd->port->cmds_to_issue[n];
  2313. size += sprintf(&buf[size], "%08X ", group_allocated);
  2314. }
  2315. size += sprintf(&buf[size], "]\n");
  2316. *offset = size <= len ? size : len;
  2317. size = copy_to_user(ubuf, buf, *offset);
  2318. if (size)
  2319. return -EFAULT;
  2320. return *offset;
  2321. }
  2322. static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
  2323. size_t len, loff_t *offset)
  2324. {
  2325. struct driver_data *dd = (struct driver_data *)f->private_data;
  2326. char buf[MTIP_DFS_MAX_BUF_SIZE];
  2327. int size = *offset;
  2328. if (!len || size)
  2329. return 0;
  2330. size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
  2331. dd->port->flags);
  2332. size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
  2333. dd->dd_flag);
  2334. *offset = size <= len ? size : len;
  2335. size = copy_to_user(ubuf, buf, *offset);
  2336. if (size)
  2337. return -EFAULT;
  2338. return *offset;
  2339. }
  2340. static const struct file_operations mtip_regs_fops = {
  2341. .owner = THIS_MODULE,
  2342. .open = simple_open,
  2343. .read = mtip_hw_read_registers,
  2344. .llseek = no_llseek,
  2345. };
  2346. static const struct file_operations mtip_flags_fops = {
  2347. .owner = THIS_MODULE,
  2348. .open = simple_open,
  2349. .read = mtip_hw_read_flags,
  2350. .llseek = no_llseek,
  2351. };
  2352. /*
  2353. * Create the sysfs related attributes.
  2354. *
  2355. * @dd Pointer to the driver data structure.
  2356. * @kobj Pointer to the kobj for the block device.
  2357. *
  2358. * return value
  2359. * 0 Operation completed successfully.
  2360. * -EINVAL Invalid parameter.
  2361. */
  2362. static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
  2363. {
  2364. if (!kobj || !dd)
  2365. return -EINVAL;
  2366. if (sysfs_create_file(kobj, &dev_attr_status.attr))
  2367. dev_warn(&dd->pdev->dev,
  2368. "Error creating 'status' sysfs entry\n");
  2369. return 0;
  2370. }
  2371. /*
  2372. * Remove the sysfs related attributes.
  2373. *
  2374. * @dd Pointer to the driver data structure.
  2375. * @kobj Pointer to the kobj for the block device.
  2376. *
  2377. * return value
  2378. * 0 Operation completed successfully.
  2379. * -EINVAL Invalid parameter.
  2380. */
  2381. static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
  2382. {
  2383. if (!kobj || !dd)
  2384. return -EINVAL;
  2385. sysfs_remove_file(kobj, &dev_attr_status.attr);
  2386. return 0;
  2387. }
  2388. static int mtip_hw_debugfs_init(struct driver_data *dd)
  2389. {
  2390. if (!dfs_parent)
  2391. return -1;
  2392. dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
  2393. if (IS_ERR_OR_NULL(dd->dfs_node)) {
  2394. dev_warn(&dd->pdev->dev,
  2395. "Error creating node %s under debugfs\n",
  2396. dd->disk->disk_name);
  2397. dd->dfs_node = NULL;
  2398. return -1;
  2399. }
  2400. debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
  2401. &mtip_flags_fops);
  2402. debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
  2403. &mtip_regs_fops);
  2404. return 0;
  2405. }
  2406. static void mtip_hw_debugfs_exit(struct driver_data *dd)
  2407. {
  2408. debugfs_remove_recursive(dd->dfs_node);
  2409. }
  2410. /*
  2411. * Perform any init/resume time hardware setup
  2412. *
  2413. * @dd Pointer to the driver data structure.
  2414. *
  2415. * return value
  2416. * None
  2417. */
  2418. static inline void hba_setup(struct driver_data *dd)
  2419. {
  2420. u32 hwdata;
  2421. hwdata = readl(dd->mmio + HOST_HSORG);
  2422. /* interrupt bug workaround: use only 1 IS bit.*/
  2423. writel(hwdata |
  2424. HSORG_DISABLE_SLOTGRP_INTR |
  2425. HSORG_DISABLE_SLOTGRP_PXIS,
  2426. dd->mmio + HOST_HSORG);
  2427. }
  2428. /*
  2429. * Detect the details of the product, and store anything needed
  2430. * into the driver data structure. This includes product type and
  2431. * version and number of slot groups.
  2432. *
  2433. * @dd Pointer to the driver data structure.
  2434. *
  2435. * return value
  2436. * None
  2437. */
  2438. static void mtip_detect_product(struct driver_data *dd)
  2439. {
  2440. u32 hwdata;
  2441. unsigned int rev, slotgroups;
  2442. /*
  2443. * HBA base + 0xFC [15:0] - vendor-specific hardware interface
  2444. * info register:
  2445. * [15:8] hardware/software interface rev#
  2446. * [ 3] asic-style interface
  2447. * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
  2448. */
  2449. hwdata = readl(dd->mmio + HOST_HSORG);
  2450. dd->product_type = MTIP_PRODUCT_UNKNOWN;
  2451. dd->slot_groups = 1;
  2452. if (hwdata & 0x8) {
  2453. dd->product_type = MTIP_PRODUCT_ASICFPGA;
  2454. rev = (hwdata & HSORG_HWREV) >> 8;
  2455. slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
  2456. dev_info(&dd->pdev->dev,
  2457. "ASIC-FPGA design, HS rev 0x%x, "
  2458. "%i slot groups [%i slots]\n",
  2459. rev,
  2460. slotgroups,
  2461. slotgroups * 32);
  2462. if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
  2463. dev_warn(&dd->pdev->dev,
  2464. "Warning: driver only supports "
  2465. "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
  2466. slotgroups = MTIP_MAX_SLOT_GROUPS;
  2467. }
  2468. dd->slot_groups = slotgroups;
  2469. return;
  2470. }
  2471. dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
  2472. }
  2473. /*
  2474. * Blocking wait for FTL rebuild to complete
  2475. *
  2476. * @dd Pointer to the DRIVER_DATA structure.
  2477. *
  2478. * return value
  2479. * 0 FTL rebuild completed successfully
  2480. * -EFAULT FTL rebuild error/timeout/interruption
  2481. */
  2482. static int mtip_ftl_rebuild_poll(struct driver_data *dd)
  2483. {
  2484. unsigned long timeout, cnt = 0, start;
  2485. dev_warn(&dd->pdev->dev,
  2486. "FTL rebuild in progress. Polling for completion.\n");
  2487. start = jiffies;
  2488. timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
  2489. do {
  2490. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  2491. &dd->dd_flag)))
  2492. return -EFAULT;
  2493. if (mtip_check_surprise_removal(dd->pdev))
  2494. return -EFAULT;
  2495. if (mtip_get_identify(dd->port, NULL) < 0)
  2496. return -EFAULT;
  2497. if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
  2498. MTIP_FTL_REBUILD_MAGIC) {
  2499. ssleep(1);
  2500. /* Print message every 3 minutes */
  2501. if (cnt++ >= 180) {
  2502. dev_warn(&dd->pdev->dev,
  2503. "FTL rebuild in progress (%d secs).\n",
  2504. jiffies_to_msecs(jiffies - start) / 1000);
  2505. cnt = 0;
  2506. }
  2507. } else {
  2508. dev_warn(&dd->pdev->dev,
  2509. "FTL rebuild complete (%d secs).\n",
  2510. jiffies_to_msecs(jiffies - start) / 1000);
  2511. mtip_block_initialize(dd);
  2512. return 0;
  2513. }
  2514. ssleep(10);
  2515. } while (time_before(jiffies, timeout));
  2516. /* Check for timeout */
  2517. dev_err(&dd->pdev->dev,
  2518. "Timed out waiting for FTL rebuild to complete (%d secs).\n",
  2519. jiffies_to_msecs(jiffies - start) / 1000);
  2520. return -EFAULT;
  2521. }
  2522. /*
  2523. * service thread to issue queued commands
  2524. *
  2525. * @data Pointer to the driver data structure.
  2526. *
  2527. * return value
  2528. * 0
  2529. */
  2530. static int mtip_service_thread(void *data)
  2531. {
  2532. struct driver_data *dd = (struct driver_data *)data;
  2533. unsigned long slot, slot_start, slot_wrap;
  2534. unsigned int num_cmd_slots = dd->slot_groups * 32;
  2535. struct mtip_port *port = dd->port;
  2536. while (1) {
  2537. /*
  2538. * the condition is to check neither an internal command is
  2539. * is in progress nor error handling is active
  2540. */
  2541. wait_event_interruptible(port->svc_wait, (port->flags) &&
  2542. !(port->flags & MTIP_PF_PAUSE_IO));
  2543. if (kthread_should_stop())
  2544. break;
  2545. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  2546. &dd->dd_flag)))
  2547. break;
  2548. set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
  2549. if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
  2550. slot = 1;
  2551. /* used to restrict the loop to one iteration */
  2552. slot_start = num_cmd_slots;
  2553. slot_wrap = 0;
  2554. while (1) {
  2555. slot = find_next_bit(port->cmds_to_issue,
  2556. num_cmd_slots, slot);
  2557. if (slot_wrap == 1) {
  2558. if ((slot_start >= slot) ||
  2559. (slot >= num_cmd_slots))
  2560. break;
  2561. }
  2562. if (unlikely(slot_start == num_cmd_slots))
  2563. slot_start = slot;
  2564. if (unlikely(slot == num_cmd_slots)) {
  2565. slot = 1;
  2566. slot_wrap = 1;
  2567. continue;
  2568. }
  2569. /* Issue the command to the hardware */
  2570. mtip_issue_ncq_command(port, slot);
  2571. clear_bit(slot, port->cmds_to_issue);
  2572. }
  2573. clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
  2574. } else if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
  2575. if (!mtip_ftl_rebuild_poll(dd))
  2576. set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
  2577. &dd->dd_flag);
  2578. clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
  2579. }
  2580. clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
  2581. if (test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
  2582. break;
  2583. }
  2584. return 0;
  2585. }
  2586. /*
  2587. * Called once for each card.
  2588. *
  2589. * @dd Pointer to the driver data structure.
  2590. *
  2591. * return value
  2592. * 0 on success, else an error code.
  2593. */
  2594. static int mtip_hw_init(struct driver_data *dd)
  2595. {
  2596. int i;
  2597. int rv;
  2598. unsigned int num_command_slots;
  2599. unsigned long timeout, timetaken;
  2600. unsigned char *buf;
  2601. struct smart_attr attr242;
  2602. dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
  2603. mtip_detect_product(dd);
  2604. if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
  2605. rv = -EIO;
  2606. goto out1;
  2607. }
  2608. num_command_slots = dd->slot_groups * 32;
  2609. hba_setup(dd);
  2610. tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
  2611. dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
  2612. if (!dd->port) {
  2613. dev_err(&dd->pdev->dev,
  2614. "Memory allocation: port structure\n");
  2615. return -ENOMEM;
  2616. }
  2617. /* Counting semaphore to track command slot usage */
  2618. sema_init(&dd->port->cmd_slot, num_command_slots - 1);
  2619. /* Spinlock to prevent concurrent issue */
  2620. spin_lock_init(&dd->port->cmd_issue_lock);
  2621. /* Set the port mmio base address. */
  2622. dd->port->mmio = dd->mmio + PORT_OFFSET;
  2623. dd->port->dd = dd;
  2624. /* Allocate memory for the command list. */
  2625. dd->port->command_list =
  2626. dmam_alloc_coherent(&dd->pdev->dev,
  2627. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
  2628. &dd->port->command_list_dma,
  2629. GFP_KERNEL);
  2630. if (!dd->port->command_list) {
  2631. dev_err(&dd->pdev->dev,
  2632. "Memory allocation: command list\n");
  2633. rv = -ENOMEM;
  2634. goto out1;
  2635. }
  2636. /* Clear the memory we have allocated. */
  2637. memset(dd->port->command_list,
  2638. 0,
  2639. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4));
  2640. /* Setup the addresse of the RX FIS. */
  2641. dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
  2642. dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
  2643. /* Setup the address of the command tables. */
  2644. dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
  2645. dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
  2646. /* Setup the address of the identify data. */
  2647. dd->port->identify = dd->port->command_table +
  2648. HW_CMD_TBL_AR_SZ;
  2649. dd->port->identify_dma = dd->port->command_tbl_dma +
  2650. HW_CMD_TBL_AR_SZ;
  2651. /* Setup the address of the sector buffer - for some non-ncq cmds */
  2652. dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
  2653. dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
  2654. /* Setup the address of the log buf - for read log command */
  2655. dd->port->log_buf = (void *)dd->port->sector_buffer + ATA_SECT_SIZE;
  2656. dd->port->log_buf_dma = dd->port->sector_buffer_dma + ATA_SECT_SIZE;
  2657. /* Setup the address of the smart buf - for smart read data command */
  2658. dd->port->smart_buf = (void *)dd->port->log_buf + ATA_SECT_SIZE;
  2659. dd->port->smart_buf_dma = dd->port->log_buf_dma + ATA_SECT_SIZE;
  2660. /* Point the command headers at the command tables. */
  2661. for (i = 0; i < num_command_slots; i++) {
  2662. dd->port->commands[i].command_header =
  2663. dd->port->command_list +
  2664. (sizeof(struct mtip_cmd_hdr) * i);
  2665. dd->port->commands[i].command_header_dma =
  2666. dd->port->command_list_dma +
  2667. (sizeof(struct mtip_cmd_hdr) * i);
  2668. dd->port->commands[i].command =
  2669. dd->port->command_table + (HW_CMD_TBL_SZ * i);
  2670. dd->port->commands[i].command_dma =
  2671. dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
  2672. if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
  2673. dd->port->commands[i].command_header->ctbau =
  2674. __force_bit2int cpu_to_le32(
  2675. (dd->port->commands[i].command_dma >> 16) >> 16);
  2676. dd->port->commands[i].command_header->ctba =
  2677. __force_bit2int cpu_to_le32(
  2678. dd->port->commands[i].command_dma & 0xFFFFFFFF);
  2679. /*
  2680. * If this is not done, a bug is reported by the stock
  2681. * FC11 i386. Due to the fact that it has lots of kernel
  2682. * debugging enabled.
  2683. */
  2684. sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
  2685. /* Mark all commands as currently inactive.*/
  2686. atomic_set(&dd->port->commands[i].active, 0);
  2687. }
  2688. /* Setup the pointers to the extended s_active and CI registers. */
  2689. for (i = 0; i < dd->slot_groups; i++) {
  2690. dd->port->s_active[i] =
  2691. dd->port->mmio + i*0x80 + PORT_SCR_ACT;
  2692. dd->port->cmd_issue[i] =
  2693. dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
  2694. dd->port->completed[i] =
  2695. dd->port->mmio + i*0x80 + PORT_SDBV;
  2696. }
  2697. timetaken = jiffies;
  2698. timeout = jiffies + msecs_to_jiffies(30000);
  2699. while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
  2700. time_before(jiffies, timeout)) {
  2701. mdelay(100);
  2702. }
  2703. if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
  2704. timetaken = jiffies - timetaken;
  2705. dev_warn(&dd->pdev->dev,
  2706. "Surprise removal detected at %u ms\n",
  2707. jiffies_to_msecs(timetaken));
  2708. rv = -ENODEV;
  2709. goto out2 ;
  2710. }
  2711. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
  2712. timetaken = jiffies - timetaken;
  2713. dev_warn(&dd->pdev->dev,
  2714. "Removal detected at %u ms\n",
  2715. jiffies_to_msecs(timetaken));
  2716. rv = -EFAULT;
  2717. goto out2;
  2718. }
  2719. /* Conditionally reset the HBA. */
  2720. if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
  2721. if (mtip_hba_reset(dd) < 0) {
  2722. dev_err(&dd->pdev->dev,
  2723. "Card did not reset within timeout\n");
  2724. rv = -EIO;
  2725. goto out2;
  2726. }
  2727. } else {
  2728. /* Clear any pending interrupts on the HBA */
  2729. writel(readl(dd->mmio + HOST_IRQ_STAT),
  2730. dd->mmio + HOST_IRQ_STAT);
  2731. }
  2732. mtip_init_port(dd->port);
  2733. mtip_start_port(dd->port);
  2734. /* Setup the ISR and enable interrupts. */
  2735. rv = devm_request_irq(&dd->pdev->dev,
  2736. dd->pdev->irq,
  2737. mtip_irq_handler,
  2738. IRQF_SHARED,
  2739. dev_driver_string(&dd->pdev->dev),
  2740. dd);
  2741. if (rv) {
  2742. dev_err(&dd->pdev->dev,
  2743. "Unable to allocate IRQ %d\n", dd->pdev->irq);
  2744. goto out2;
  2745. }
  2746. /* Enable interrupts on the HBA. */
  2747. writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
  2748. dd->mmio + HOST_CTL);
  2749. init_timer(&dd->port->cmd_timer);
  2750. init_waitqueue_head(&dd->port->svc_wait);
  2751. dd->port->cmd_timer.data = (unsigned long int) dd->port;
  2752. dd->port->cmd_timer.function = mtip_timeout_function;
  2753. mod_timer(&dd->port->cmd_timer,
  2754. jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
  2755. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
  2756. rv = -EFAULT;
  2757. goto out3;
  2758. }
  2759. if (mtip_get_identify(dd->port, NULL) < 0) {
  2760. rv = -EFAULT;
  2761. goto out3;
  2762. }
  2763. if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
  2764. MTIP_FTL_REBUILD_MAGIC) {
  2765. set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
  2766. return MTIP_FTL_REBUILD_MAGIC;
  2767. }
  2768. mtip_dump_identify(dd->port);
  2769. /* check write protect, over temp and rebuild statuses */
  2770. rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
  2771. dd->port->log_buf,
  2772. dd->port->log_buf_dma, 1);
  2773. if (rv) {
  2774. dev_warn(&dd->pdev->dev,
  2775. "Error in READ LOG EXT (10h) command\n");
  2776. /* non-critical error, don't fail the load */
  2777. } else {
  2778. buf = (unsigned char *)dd->port->log_buf;
  2779. if (buf[259] & 0x1) {
  2780. dev_info(&dd->pdev->dev,
  2781. "Write protect bit is set.\n");
  2782. set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
  2783. }
  2784. if (buf[288] == 0xF7) {
  2785. dev_info(&dd->pdev->dev,
  2786. "Exceeded Tmax, drive in thermal shutdown.\n");
  2787. set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
  2788. }
  2789. if (buf[288] == 0xBF) {
  2790. dev_info(&dd->pdev->dev,
  2791. "Drive indicates rebuild has failed.\n");
  2792. /* TODO */
  2793. }
  2794. }
  2795. /* get write protect progess */
  2796. memset(&attr242, 0, sizeof(struct smart_attr));
  2797. if (mtip_get_smart_attr(dd->port, 242, &attr242))
  2798. dev_warn(&dd->pdev->dev,
  2799. "Unable to check write protect progress\n");
  2800. else
  2801. dev_info(&dd->pdev->dev,
  2802. "Write protect progress: %u%% (%u blocks)\n",
  2803. attr242.cur, le32_to_cpu(attr242.data));
  2804. return rv;
  2805. out3:
  2806. del_timer_sync(&dd->port->cmd_timer);
  2807. /* Disable interrupts on the HBA. */
  2808. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2809. dd->mmio + HOST_CTL);
  2810. /*Release the IRQ. */
  2811. devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
  2812. out2:
  2813. mtip_deinit_port(dd->port);
  2814. /* Free the command/command header memory. */
  2815. dmam_free_coherent(&dd->pdev->dev,
  2816. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
  2817. dd->port->command_list,
  2818. dd->port->command_list_dma);
  2819. out1:
  2820. /* Free the memory allocated for the for structure. */
  2821. kfree(dd->port);
  2822. return rv;
  2823. }
  2824. /*
  2825. * Called to deinitialize an interface.
  2826. *
  2827. * @dd Pointer to the driver data structure.
  2828. *
  2829. * return value
  2830. * 0
  2831. */
  2832. static int mtip_hw_exit(struct driver_data *dd)
  2833. {
  2834. /*
  2835. * Send standby immediate (E0h) to the drive so that it
  2836. * saves its state.
  2837. */
  2838. if (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
  2839. if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags))
  2840. if (mtip_standby_immediate(dd->port))
  2841. dev_warn(&dd->pdev->dev,
  2842. "STANDBY IMMEDIATE failed\n");
  2843. /* de-initialize the port. */
  2844. mtip_deinit_port(dd->port);
  2845. /* Disable interrupts on the HBA. */
  2846. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2847. dd->mmio + HOST_CTL);
  2848. }
  2849. del_timer_sync(&dd->port->cmd_timer);
  2850. /* Release the IRQ. */
  2851. devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
  2852. /* Stop the bottom half tasklet. */
  2853. tasklet_kill(&dd->tasklet);
  2854. /* Free the command/command header memory. */
  2855. dmam_free_coherent(&dd->pdev->dev,
  2856. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
  2857. dd->port->command_list,
  2858. dd->port->command_list_dma);
  2859. /* Free the memory allocated for the for structure. */
  2860. kfree(dd->port);
  2861. return 0;
  2862. }
  2863. /*
  2864. * Issue a Standby Immediate command to the device.
  2865. *
  2866. * This function is called by the Block Layer just before the
  2867. * system powers off during a shutdown.
  2868. *
  2869. * @dd Pointer to the driver data structure.
  2870. *
  2871. * return value
  2872. * 0
  2873. */
  2874. static int mtip_hw_shutdown(struct driver_data *dd)
  2875. {
  2876. /*
  2877. * Send standby immediate (E0h) to the drive so that it
  2878. * saves its state.
  2879. */
  2880. mtip_standby_immediate(dd->port);
  2881. return 0;
  2882. }
  2883. /*
  2884. * Suspend function
  2885. *
  2886. * This function is called by the Block Layer just before the
  2887. * system hibernates.
  2888. *
  2889. * @dd Pointer to the driver data structure.
  2890. *
  2891. * return value
  2892. * 0 Suspend was successful
  2893. * -EFAULT Suspend was not successful
  2894. */
  2895. static int mtip_hw_suspend(struct driver_data *dd)
  2896. {
  2897. /*
  2898. * Send standby immediate (E0h) to the drive
  2899. * so that it saves its state.
  2900. */
  2901. if (mtip_standby_immediate(dd->port) != 0) {
  2902. dev_err(&dd->pdev->dev,
  2903. "Failed standby-immediate command\n");
  2904. return -EFAULT;
  2905. }
  2906. /* Disable interrupts on the HBA.*/
  2907. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2908. dd->mmio + HOST_CTL);
  2909. mtip_deinit_port(dd->port);
  2910. return 0;
  2911. }
  2912. /*
  2913. * Resume function
  2914. *
  2915. * This function is called by the Block Layer as the
  2916. * system resumes.
  2917. *
  2918. * @dd Pointer to the driver data structure.
  2919. *
  2920. * return value
  2921. * 0 Resume was successful
  2922. * -EFAULT Resume was not successful
  2923. */
  2924. static int mtip_hw_resume(struct driver_data *dd)
  2925. {
  2926. /* Perform any needed hardware setup steps */
  2927. hba_setup(dd);
  2928. /* Reset the HBA */
  2929. if (mtip_hba_reset(dd) != 0) {
  2930. dev_err(&dd->pdev->dev,
  2931. "Unable to reset the HBA\n");
  2932. return -EFAULT;
  2933. }
  2934. /*
  2935. * Enable the port, DMA engine, and FIS reception specific
  2936. * h/w in controller.
  2937. */
  2938. mtip_init_port(dd->port);
  2939. mtip_start_port(dd->port);
  2940. /* Enable interrupts on the HBA.*/
  2941. writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
  2942. dd->mmio + HOST_CTL);
  2943. return 0;
  2944. }
  2945. /*
  2946. * Helper function for reusing disk name
  2947. * upon hot insertion.
  2948. */
  2949. static int rssd_disk_name_format(char *prefix,
  2950. int index,
  2951. char *buf,
  2952. int buflen)
  2953. {
  2954. const int base = 'z' - 'a' + 1;
  2955. char *begin = buf + strlen(prefix);
  2956. char *end = buf + buflen;
  2957. char *p;
  2958. int unit;
  2959. p = end - 1;
  2960. *p = '\0';
  2961. unit = base;
  2962. do {
  2963. if (p == begin)
  2964. return -EINVAL;
  2965. *--p = 'a' + (index % unit);
  2966. index = (index / unit) - 1;
  2967. } while (index >= 0);
  2968. memmove(begin, p, end - p);
  2969. memcpy(buf, prefix, strlen(prefix));
  2970. return 0;
  2971. }
  2972. /*
  2973. * Block layer IOCTL handler.
  2974. *
  2975. * @dev Pointer to the block_device structure.
  2976. * @mode ignored
  2977. * @cmd IOCTL command passed from the user application.
  2978. * @arg Argument passed from the user application.
  2979. *
  2980. * return value
  2981. * 0 IOCTL completed successfully.
  2982. * -ENOTTY IOCTL not supported or invalid driver data
  2983. * structure pointer.
  2984. */
  2985. static int mtip_block_ioctl(struct block_device *dev,
  2986. fmode_t mode,
  2987. unsigned cmd,
  2988. unsigned long arg)
  2989. {
  2990. struct driver_data *dd = dev->bd_disk->private_data;
  2991. if (!capable(CAP_SYS_ADMIN))
  2992. return -EACCES;
  2993. if (!dd)
  2994. return -ENOTTY;
  2995. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
  2996. return -ENOTTY;
  2997. switch (cmd) {
  2998. case BLKFLSBUF:
  2999. return -ENOTTY;
  3000. default:
  3001. return mtip_hw_ioctl(dd, cmd, arg);
  3002. }
  3003. }
  3004. #ifdef CONFIG_COMPAT
  3005. /*
  3006. * Block layer compat IOCTL handler.
  3007. *
  3008. * @dev Pointer to the block_device structure.
  3009. * @mode ignored
  3010. * @cmd IOCTL command passed from the user application.
  3011. * @arg Argument passed from the user application.
  3012. *
  3013. * return value
  3014. * 0 IOCTL completed successfully.
  3015. * -ENOTTY IOCTL not supported or invalid driver data
  3016. * structure pointer.
  3017. */
  3018. static int mtip_block_compat_ioctl(struct block_device *dev,
  3019. fmode_t mode,
  3020. unsigned cmd,
  3021. unsigned long arg)
  3022. {
  3023. struct driver_data *dd = dev->bd_disk->private_data;
  3024. if (!capable(CAP_SYS_ADMIN))
  3025. return -EACCES;
  3026. if (!dd)
  3027. return -ENOTTY;
  3028. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
  3029. return -ENOTTY;
  3030. switch (cmd) {
  3031. case BLKFLSBUF:
  3032. return -ENOTTY;
  3033. case HDIO_DRIVE_TASKFILE: {
  3034. struct mtip_compat_ide_task_request_s __user *compat_req_task;
  3035. ide_task_request_t req_task;
  3036. int compat_tasksize, outtotal, ret;
  3037. compat_tasksize =
  3038. sizeof(struct mtip_compat_ide_task_request_s);
  3039. compat_req_task =
  3040. (struct mtip_compat_ide_task_request_s __user *) arg;
  3041. if (copy_from_user(&req_task, (void __user *) arg,
  3042. compat_tasksize - (2 * sizeof(compat_long_t))))
  3043. return -EFAULT;
  3044. if (get_user(req_task.out_size, &compat_req_task->out_size))
  3045. return -EFAULT;
  3046. if (get_user(req_task.in_size, &compat_req_task->in_size))
  3047. return -EFAULT;
  3048. outtotal = sizeof(struct mtip_compat_ide_task_request_s);
  3049. ret = exec_drive_taskfile(dd, (void __user *) arg,
  3050. &req_task, outtotal);
  3051. if (copy_to_user((void __user *) arg, &req_task,
  3052. compat_tasksize -
  3053. (2 * sizeof(compat_long_t))))
  3054. return -EFAULT;
  3055. if (put_user(req_task.out_size, &compat_req_task->out_size))
  3056. return -EFAULT;
  3057. if (put_user(req_task.in_size, &compat_req_task->in_size))
  3058. return -EFAULT;
  3059. return ret;
  3060. }
  3061. default:
  3062. return mtip_hw_ioctl(dd, cmd, arg);
  3063. }
  3064. }
  3065. #endif
  3066. /*
  3067. * Obtain the geometry of the device.
  3068. *
  3069. * You may think that this function is obsolete, but some applications,
  3070. * fdisk for example still used CHS values. This function describes the
  3071. * device as having 224 heads and 56 sectors per cylinder. These values are
  3072. * chosen so that each cylinder is aligned on a 4KB boundary. Since a
  3073. * partition is described in terms of a start and end cylinder this means
  3074. * that each partition is also 4KB aligned. Non-aligned partitions adversely
  3075. * affects performance.
  3076. *
  3077. * @dev Pointer to the block_device strucutre.
  3078. * @geo Pointer to a hd_geometry structure.
  3079. *
  3080. * return value
  3081. * 0 Operation completed successfully.
  3082. * -ENOTTY An error occurred while reading the drive capacity.
  3083. */
  3084. static int mtip_block_getgeo(struct block_device *dev,
  3085. struct hd_geometry *geo)
  3086. {
  3087. struct driver_data *dd = dev->bd_disk->private_data;
  3088. sector_t capacity;
  3089. if (!dd)
  3090. return -ENOTTY;
  3091. if (!(mtip_hw_get_capacity(dd, &capacity))) {
  3092. dev_warn(&dd->pdev->dev,
  3093. "Could not get drive capacity.\n");
  3094. return -ENOTTY;
  3095. }
  3096. geo->heads = 224;
  3097. geo->sectors = 56;
  3098. sector_div(capacity, (geo->heads * geo->sectors));
  3099. geo->cylinders = capacity;
  3100. return 0;
  3101. }
  3102. /*
  3103. * Block device operation function.
  3104. *
  3105. * This structure contains pointers to the functions required by the block
  3106. * layer.
  3107. */
  3108. static const struct block_device_operations mtip_block_ops = {
  3109. .ioctl = mtip_block_ioctl,
  3110. #ifdef CONFIG_COMPAT
  3111. .compat_ioctl = mtip_block_compat_ioctl,
  3112. #endif
  3113. .getgeo = mtip_block_getgeo,
  3114. .owner = THIS_MODULE
  3115. };
  3116. /*
  3117. * Block layer make request function.
  3118. *
  3119. * This function is called by the kernel to process a BIO for
  3120. * the P320 device.
  3121. *
  3122. * @queue Pointer to the request queue. Unused other than to obtain
  3123. * the driver data structure.
  3124. * @bio Pointer to the BIO.
  3125. *
  3126. */
  3127. static void mtip_make_request(struct request_queue *queue, struct bio *bio)
  3128. {
  3129. struct driver_data *dd = queue->queuedata;
  3130. struct scatterlist *sg;
  3131. struct bio_vec *bvec;
  3132. int nents = 0;
  3133. int tag = 0;
  3134. if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
  3135. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  3136. &dd->dd_flag))) {
  3137. bio_endio(bio, -ENXIO);
  3138. return;
  3139. }
  3140. if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
  3141. bio_endio(bio, -ENODATA);
  3142. return;
  3143. }
  3144. if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
  3145. &dd->dd_flag) &&
  3146. bio_data_dir(bio))) {
  3147. bio_endio(bio, -ENODATA);
  3148. return;
  3149. }
  3150. if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))) {
  3151. bio_endio(bio, -ENODATA);
  3152. return;
  3153. }
  3154. }
  3155. if (unlikely(!bio_has_data(bio))) {
  3156. blk_queue_flush(queue, 0);
  3157. bio_endio(bio, 0);
  3158. return;
  3159. }
  3160. sg = mtip_hw_get_scatterlist(dd, &tag);
  3161. if (likely(sg != NULL)) {
  3162. blk_queue_bounce(queue, &bio);
  3163. if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
  3164. dev_warn(&dd->pdev->dev,
  3165. "Maximum number of SGL entries exceeded\n");
  3166. bio_io_error(bio);
  3167. mtip_hw_release_scatterlist(dd, tag);
  3168. return;
  3169. }
  3170. /* Create the scatter list for this bio. */
  3171. bio_for_each_segment(bvec, bio, nents) {
  3172. sg_set_page(&sg[nents],
  3173. bvec->bv_page,
  3174. bvec->bv_len,
  3175. bvec->bv_offset);
  3176. }
  3177. /* Issue the read/write. */
  3178. mtip_hw_submit_io(dd,
  3179. bio->bi_sector,
  3180. bio_sectors(bio),
  3181. nents,
  3182. tag,
  3183. bio_endio,
  3184. bio,
  3185. bio_data_dir(bio));
  3186. } else
  3187. bio_io_error(bio);
  3188. }
  3189. /*
  3190. * Block layer initialization function.
  3191. *
  3192. * This function is called once by the PCI layer for each P320
  3193. * device that is connected to the system.
  3194. *
  3195. * @dd Pointer to the driver data structure.
  3196. *
  3197. * return value
  3198. * 0 on success else an error code.
  3199. */
  3200. static int mtip_block_initialize(struct driver_data *dd)
  3201. {
  3202. int rv = 0, wait_for_rebuild = 0;
  3203. sector_t capacity;
  3204. unsigned int index = 0;
  3205. struct kobject *kobj;
  3206. unsigned char thd_name[16];
  3207. if (dd->disk)
  3208. goto skip_create_disk; /* hw init done, before rebuild */
  3209. /* Initialize the protocol layer. */
  3210. wait_for_rebuild = mtip_hw_init(dd);
  3211. if (wait_for_rebuild < 0) {
  3212. dev_err(&dd->pdev->dev,
  3213. "Protocol layer initialization failed\n");
  3214. rv = -EINVAL;
  3215. goto protocol_init_error;
  3216. }
  3217. dd->disk = alloc_disk(MTIP_MAX_MINORS);
  3218. if (dd->disk == NULL) {
  3219. dev_err(&dd->pdev->dev,
  3220. "Unable to allocate gendisk structure\n");
  3221. rv = -EINVAL;
  3222. goto alloc_disk_error;
  3223. }
  3224. /* Generate the disk name, implemented same as in sd.c */
  3225. do {
  3226. if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
  3227. goto ida_get_error;
  3228. spin_lock(&rssd_index_lock);
  3229. rv = ida_get_new(&rssd_index_ida, &index);
  3230. spin_unlock(&rssd_index_lock);
  3231. } while (rv == -EAGAIN);
  3232. if (rv)
  3233. goto ida_get_error;
  3234. rv = rssd_disk_name_format("rssd",
  3235. index,
  3236. dd->disk->disk_name,
  3237. DISK_NAME_LEN);
  3238. if (rv)
  3239. goto disk_index_error;
  3240. dd->disk->driverfs_dev = &dd->pdev->dev;
  3241. dd->disk->major = dd->major;
  3242. dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
  3243. dd->disk->fops = &mtip_block_ops;
  3244. dd->disk->private_data = dd;
  3245. dd->index = index;
  3246. /*
  3247. * if rebuild pending, start the service thread, and delay the block
  3248. * queue creation and add_disk()
  3249. */
  3250. if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
  3251. goto start_service_thread;
  3252. skip_create_disk:
  3253. /* Allocate the request queue. */
  3254. dd->queue = blk_alloc_queue(GFP_KERNEL);
  3255. if (dd->queue == NULL) {
  3256. dev_err(&dd->pdev->dev,
  3257. "Unable to allocate request queue\n");
  3258. rv = -ENOMEM;
  3259. goto block_queue_alloc_init_error;
  3260. }
  3261. /* Attach our request function to the request queue. */
  3262. blk_queue_make_request(dd->queue, mtip_make_request);
  3263. dd->disk->queue = dd->queue;
  3264. dd->queue->queuedata = dd;
  3265. /* Set device limits. */
  3266. set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
  3267. blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
  3268. blk_queue_physical_block_size(dd->queue, 4096);
  3269. blk_queue_max_hw_sectors(dd->queue, 0xffff);
  3270. blk_queue_max_segment_size(dd->queue, 0x400000);
  3271. blk_queue_io_min(dd->queue, 4096);
  3272. /*
  3273. * write back cache is not supported in the device. FUA depends on
  3274. * write back cache support, hence setting flush support to zero.
  3275. */
  3276. blk_queue_flush(dd->queue, 0);
  3277. /* Set the capacity of the device in 512 byte sectors. */
  3278. if (!(mtip_hw_get_capacity(dd, &capacity))) {
  3279. dev_warn(&dd->pdev->dev,
  3280. "Could not read drive capacity\n");
  3281. rv = -EIO;
  3282. goto read_capacity_error;
  3283. }
  3284. set_capacity(dd->disk, capacity);
  3285. /* Enable the block device and add it to /dev */
  3286. add_disk(dd->disk);
  3287. /*
  3288. * Now that the disk is active, initialize any sysfs attributes
  3289. * managed by the protocol layer.
  3290. */
  3291. kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
  3292. if (kobj) {
  3293. mtip_hw_sysfs_init(dd, kobj);
  3294. kobject_put(kobj);
  3295. }
  3296. mtip_hw_debugfs_init(dd);
  3297. if (dd->mtip_svc_handler) {
  3298. set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
  3299. return rv; /* service thread created for handling rebuild */
  3300. }
  3301. start_service_thread:
  3302. sprintf(thd_name, "mtip_svc_thd_%02d", index);
  3303. dd->mtip_svc_handler = kthread_run(mtip_service_thread,
  3304. dd, thd_name);
  3305. if (IS_ERR(dd->mtip_svc_handler)) {
  3306. dev_err(&dd->pdev->dev, "service thread failed to start\n");
  3307. dd->mtip_svc_handler = NULL;
  3308. rv = -EFAULT;
  3309. goto kthread_run_error;
  3310. }
  3311. if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
  3312. rv = wait_for_rebuild;
  3313. return rv;
  3314. kthread_run_error:
  3315. mtip_hw_debugfs_exit(dd);
  3316. /* Delete our gendisk. This also removes the device from /dev */
  3317. del_gendisk(dd->disk);
  3318. read_capacity_error:
  3319. blk_cleanup_queue(dd->queue);
  3320. block_queue_alloc_init_error:
  3321. disk_index_error:
  3322. spin_lock(&rssd_index_lock);
  3323. ida_remove(&rssd_index_ida, index);
  3324. spin_unlock(&rssd_index_lock);
  3325. ida_get_error:
  3326. put_disk(dd->disk);
  3327. alloc_disk_error:
  3328. mtip_hw_exit(dd); /* De-initialize the protocol layer. */
  3329. protocol_init_error:
  3330. return rv;
  3331. }
  3332. /*
  3333. * Block layer deinitialization function.
  3334. *
  3335. * Called by the PCI layer as each P320 device is removed.
  3336. *
  3337. * @dd Pointer to the driver data structure.
  3338. *
  3339. * return value
  3340. * 0
  3341. */
  3342. static int mtip_block_remove(struct driver_data *dd)
  3343. {
  3344. struct kobject *kobj;
  3345. if (dd->mtip_svc_handler) {
  3346. set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
  3347. wake_up_interruptible(&dd->port->svc_wait);
  3348. kthread_stop(dd->mtip_svc_handler);
  3349. }
  3350. /* Clean up the sysfs attributes, if created */
  3351. if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
  3352. kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
  3353. if (kobj) {
  3354. mtip_hw_sysfs_exit(dd, kobj);
  3355. kobject_put(kobj);
  3356. }
  3357. }
  3358. mtip_hw_debugfs_exit(dd);
  3359. /*
  3360. * Delete our gendisk structure. This also removes the device
  3361. * from /dev
  3362. */
  3363. del_gendisk(dd->disk);
  3364. spin_lock(&rssd_index_lock);
  3365. ida_remove(&rssd_index_ida, dd->index);
  3366. spin_unlock(&rssd_index_lock);
  3367. blk_cleanup_queue(dd->queue);
  3368. dd->disk = NULL;
  3369. dd->queue = NULL;
  3370. /* De-initialize the protocol layer. */
  3371. mtip_hw_exit(dd);
  3372. return 0;
  3373. }
  3374. /*
  3375. * Function called by the PCI layer when just before the
  3376. * machine shuts down.
  3377. *
  3378. * If a protocol layer shutdown function is present it will be called
  3379. * by this function.
  3380. *
  3381. * @dd Pointer to the driver data structure.
  3382. *
  3383. * return value
  3384. * 0
  3385. */
  3386. static int mtip_block_shutdown(struct driver_data *dd)
  3387. {
  3388. dev_info(&dd->pdev->dev,
  3389. "Shutting down %s ...\n", dd->disk->disk_name);
  3390. /* Delete our gendisk structure, and cleanup the blk queue. */
  3391. del_gendisk(dd->disk);
  3392. spin_lock(&rssd_index_lock);
  3393. ida_remove(&rssd_index_ida, dd->index);
  3394. spin_unlock(&rssd_index_lock);
  3395. blk_cleanup_queue(dd->queue);
  3396. dd->disk = NULL;
  3397. dd->queue = NULL;
  3398. mtip_hw_shutdown(dd);
  3399. return 0;
  3400. }
  3401. static int mtip_block_suspend(struct driver_data *dd)
  3402. {
  3403. dev_info(&dd->pdev->dev,
  3404. "Suspending %s ...\n", dd->disk->disk_name);
  3405. mtip_hw_suspend(dd);
  3406. return 0;
  3407. }
  3408. static int mtip_block_resume(struct driver_data *dd)
  3409. {
  3410. dev_info(&dd->pdev->dev, "Resuming %s ...\n",
  3411. dd->disk->disk_name);
  3412. mtip_hw_resume(dd);
  3413. return 0;
  3414. }
  3415. /*
  3416. * Called for each supported PCI device detected.
  3417. *
  3418. * This function allocates the private data structure, enables the
  3419. * PCI device and then calls the block layer initialization function.
  3420. *
  3421. * return value
  3422. * 0 on success else an error code.
  3423. */
  3424. static int mtip_pci_probe(struct pci_dev *pdev,
  3425. const struct pci_device_id *ent)
  3426. {
  3427. int rv = 0;
  3428. struct driver_data *dd = NULL;
  3429. /* Allocate memory for this devices private data. */
  3430. dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
  3431. if (dd == NULL) {
  3432. dev_err(&pdev->dev,
  3433. "Unable to allocate memory for driver data\n");
  3434. return -ENOMEM;
  3435. }
  3436. /* Attach the private data to this PCI device. */
  3437. pci_set_drvdata(pdev, dd);
  3438. rv = pcim_enable_device(pdev);
  3439. if (rv < 0) {
  3440. dev_err(&pdev->dev, "Unable to enable device\n");
  3441. goto iomap_err;
  3442. }
  3443. /* Map BAR5 to memory. */
  3444. rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
  3445. if (rv < 0) {
  3446. dev_err(&pdev->dev, "Unable to map regions\n");
  3447. goto iomap_err;
  3448. }
  3449. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  3450. rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  3451. if (rv) {
  3452. rv = pci_set_consistent_dma_mask(pdev,
  3453. DMA_BIT_MASK(32));
  3454. if (rv) {
  3455. dev_warn(&pdev->dev,
  3456. "64-bit DMA enable failed\n");
  3457. goto setmask_err;
  3458. }
  3459. }
  3460. }
  3461. pci_set_master(pdev);
  3462. if (pci_enable_msi(pdev)) {
  3463. dev_warn(&pdev->dev,
  3464. "Unable to enable MSI interrupt.\n");
  3465. goto block_initialize_err;
  3466. }
  3467. /* Copy the info we may need later into the private data structure. */
  3468. dd->major = mtip_major;
  3469. dd->instance = instance;
  3470. dd->pdev = pdev;
  3471. /* Initialize the block layer. */
  3472. rv = mtip_block_initialize(dd);
  3473. if (rv < 0) {
  3474. dev_err(&pdev->dev,
  3475. "Unable to initialize block layer\n");
  3476. goto block_initialize_err;
  3477. }
  3478. /*
  3479. * Increment the instance count so that each device has a unique
  3480. * instance number.
  3481. */
  3482. instance++;
  3483. if (rv != MTIP_FTL_REBUILD_MAGIC)
  3484. set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
  3485. goto done;
  3486. block_initialize_err:
  3487. pci_disable_msi(pdev);
  3488. setmask_err:
  3489. pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
  3490. iomap_err:
  3491. kfree(dd);
  3492. pci_set_drvdata(pdev, NULL);
  3493. return rv;
  3494. done:
  3495. return rv;
  3496. }
  3497. /*
  3498. * Called for each probed device when the device is removed or the
  3499. * driver is unloaded.
  3500. *
  3501. * return value
  3502. * None
  3503. */
  3504. static void mtip_pci_remove(struct pci_dev *pdev)
  3505. {
  3506. struct driver_data *dd = pci_get_drvdata(pdev);
  3507. int counter = 0;
  3508. set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
  3509. if (mtip_check_surprise_removal(pdev)) {
  3510. while (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
  3511. counter++;
  3512. msleep(20);
  3513. if (counter == 10) {
  3514. /* Cleanup the outstanding commands */
  3515. mtip_command_cleanup(dd);
  3516. break;
  3517. }
  3518. }
  3519. }
  3520. /* Clean up the block layer. */
  3521. mtip_block_remove(dd);
  3522. pci_disable_msi(pdev);
  3523. kfree(dd);
  3524. pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
  3525. }
  3526. /*
  3527. * Called for each probed device when the device is suspended.
  3528. *
  3529. * return value
  3530. * 0 Success
  3531. * <0 Error
  3532. */
  3533. static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
  3534. {
  3535. int rv = 0;
  3536. struct driver_data *dd = pci_get_drvdata(pdev);
  3537. if (!dd) {
  3538. dev_err(&pdev->dev,
  3539. "Driver private datastructure is NULL\n");
  3540. return -EFAULT;
  3541. }
  3542. set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
  3543. /* Disable ports & interrupts then send standby immediate */
  3544. rv = mtip_block_suspend(dd);
  3545. if (rv < 0) {
  3546. dev_err(&pdev->dev,
  3547. "Failed to suspend controller\n");
  3548. return rv;
  3549. }
  3550. /*
  3551. * Save the pci config space to pdev structure &
  3552. * disable the device
  3553. */
  3554. pci_save_state(pdev);
  3555. pci_disable_device(pdev);
  3556. /* Move to Low power state*/
  3557. pci_set_power_state(pdev, PCI_D3hot);
  3558. return rv;
  3559. }
  3560. /*
  3561. * Called for each probed device when the device is resumed.
  3562. *
  3563. * return value
  3564. * 0 Success
  3565. * <0 Error
  3566. */
  3567. static int mtip_pci_resume(struct pci_dev *pdev)
  3568. {
  3569. int rv = 0;
  3570. struct driver_data *dd;
  3571. dd = pci_get_drvdata(pdev);
  3572. if (!dd) {
  3573. dev_err(&pdev->dev,
  3574. "Driver private datastructure is NULL\n");
  3575. return -EFAULT;
  3576. }
  3577. /* Move the device to active State */
  3578. pci_set_power_state(pdev, PCI_D0);
  3579. /* Restore PCI configuration space */
  3580. pci_restore_state(pdev);
  3581. /* Enable the PCI device*/
  3582. rv = pcim_enable_device(pdev);
  3583. if (rv < 0) {
  3584. dev_err(&pdev->dev,
  3585. "Failed to enable card during resume\n");
  3586. goto err;
  3587. }
  3588. pci_set_master(pdev);
  3589. /*
  3590. * Calls hbaReset, initPort, & startPort function
  3591. * then enables interrupts
  3592. */
  3593. rv = mtip_block_resume(dd);
  3594. if (rv < 0)
  3595. dev_err(&pdev->dev, "Unable to resume\n");
  3596. err:
  3597. clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
  3598. return rv;
  3599. }
  3600. /*
  3601. * Shutdown routine
  3602. *
  3603. * return value
  3604. * None
  3605. */
  3606. static void mtip_pci_shutdown(struct pci_dev *pdev)
  3607. {
  3608. struct driver_data *dd = pci_get_drvdata(pdev);
  3609. if (dd)
  3610. mtip_block_shutdown(dd);
  3611. }
  3612. /* Table of device ids supported by this driver. */
  3613. static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
  3614. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
  3615. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
  3616. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
  3617. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
  3618. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
  3619. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
  3620. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
  3621. { 0 }
  3622. };
  3623. /* Structure that describes the PCI driver functions. */
  3624. static struct pci_driver mtip_pci_driver = {
  3625. .name = MTIP_DRV_NAME,
  3626. .id_table = mtip_pci_tbl,
  3627. .probe = mtip_pci_probe,
  3628. .remove = mtip_pci_remove,
  3629. .suspend = mtip_pci_suspend,
  3630. .resume = mtip_pci_resume,
  3631. .shutdown = mtip_pci_shutdown,
  3632. };
  3633. MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
  3634. /*
  3635. * Module initialization function.
  3636. *
  3637. * Called once when the module is loaded. This function allocates a major
  3638. * block device number to the Cyclone devices and registers the PCI layer
  3639. * of the driver.
  3640. *
  3641. * Return value
  3642. * 0 on success else error code.
  3643. */
  3644. static int __init mtip_init(void)
  3645. {
  3646. int error;
  3647. pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
  3648. /* Allocate a major block device number to use with this driver. */
  3649. error = register_blkdev(0, MTIP_DRV_NAME);
  3650. if (error <= 0) {
  3651. pr_err("Unable to register block device (%d)\n",
  3652. error);
  3653. return -EBUSY;
  3654. }
  3655. mtip_major = error;
  3656. if (!dfs_parent) {
  3657. dfs_parent = debugfs_create_dir("rssd", NULL);
  3658. if (IS_ERR_OR_NULL(dfs_parent)) {
  3659. pr_warn("Error creating debugfs parent\n");
  3660. dfs_parent = NULL;
  3661. }
  3662. }
  3663. /* Register our PCI operations. */
  3664. error = pci_register_driver(&mtip_pci_driver);
  3665. if (error) {
  3666. debugfs_remove(dfs_parent);
  3667. unregister_blkdev(mtip_major, MTIP_DRV_NAME);
  3668. }
  3669. return error;
  3670. }
  3671. /*
  3672. * Module de-initialization function.
  3673. *
  3674. * Called once when the module is unloaded. This function deallocates
  3675. * the major block device number allocated by mtip_init() and
  3676. * unregisters the PCI layer of the driver.
  3677. *
  3678. * Return value
  3679. * none
  3680. */
  3681. static void __exit mtip_exit(void)
  3682. {
  3683. debugfs_remove_recursive(dfs_parent);
  3684. /* Release the allocated major block device number. */
  3685. unregister_blkdev(mtip_major, MTIP_DRV_NAME);
  3686. /* Unregister the PCI driver. */
  3687. pci_unregister_driver(&mtip_pci_driver);
  3688. }
  3689. MODULE_AUTHOR("Micron Technology, Inc");
  3690. MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
  3691. MODULE_LICENSE("GPL");
  3692. MODULE_VERSION(MTIP_DRV_VERSION);
  3693. module_init(mtip_init);
  3694. module_exit(mtip_exit);