mtip32xx.c 99 KB

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