mtip32xx.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759
  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_DD_FLAG_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_DD_FLAG_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->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
  380. /* Clear any pending interrupts on the HBA. */
  381. writel(readl(port->dd->mmio + HOST_IRQ_STAT),
  382. port->dd->mmio + HOST_IRQ_STAT);
  383. /* Enable port interrupts */
  384. writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
  385. }
  386. /*
  387. * Restart a port
  388. *
  389. * @port Pointer to the port data structure.
  390. *
  391. * return value
  392. * None
  393. */
  394. static void mtip_restart_port(struct mtip_port *port)
  395. {
  396. unsigned long timeout;
  397. /* Disable the DMA engine */
  398. mtip_enable_engine(port, 0);
  399. /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
  400. timeout = jiffies + msecs_to_jiffies(500);
  401. while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
  402. && time_before(jiffies, timeout))
  403. ;
  404. if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  405. return;
  406. /*
  407. * Chip quirk: escalate to hba reset if
  408. * PxCMD.CR not clear after 500 ms
  409. */
  410. if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
  411. dev_warn(&port->dd->pdev->dev,
  412. "PxCMD.CR not clear, escalating reset\n");
  413. if (hba_reset_nosleep(port->dd))
  414. dev_err(&port->dd->pdev->dev,
  415. "HBA reset escalation failed.\n");
  416. /* 30 ms delay before com reset to quiesce chip */
  417. mdelay(30);
  418. }
  419. dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
  420. /* Set PxSCTL.DET */
  421. writel(readl(port->mmio + PORT_SCR_CTL) |
  422. 1, port->mmio + PORT_SCR_CTL);
  423. readl(port->mmio + PORT_SCR_CTL);
  424. /* Wait 1 ms to quiesce chip function */
  425. timeout = jiffies + msecs_to_jiffies(1);
  426. while (time_before(jiffies, timeout))
  427. ;
  428. if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  429. return;
  430. /* Clear PxSCTL.DET */
  431. writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
  432. port->mmio + PORT_SCR_CTL);
  433. readl(port->mmio + PORT_SCR_CTL);
  434. /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
  435. timeout = jiffies + msecs_to_jiffies(500);
  436. while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
  437. && time_before(jiffies, timeout))
  438. ;
  439. if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  440. return;
  441. if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
  442. dev_warn(&port->dd->pdev->dev,
  443. "COM reset failed\n");
  444. mtip_init_port(port);
  445. mtip_start_port(port);
  446. }
  447. /*
  448. * Called periodically to see if any read/write commands are
  449. * taking too long to complete.
  450. *
  451. * @data Pointer to the PORT data structure.
  452. *
  453. * return value
  454. * None
  455. */
  456. static void mtip_timeout_function(unsigned long int data)
  457. {
  458. struct mtip_port *port = (struct mtip_port *) data;
  459. struct host_to_dev_fis *fis;
  460. struct mtip_cmd *command;
  461. int tag, cmdto_cnt = 0;
  462. unsigned int bit, group;
  463. unsigned int num_command_slots = port->dd->slot_groups * 32;
  464. if (unlikely(!port))
  465. return;
  466. if (test_bit(MTIP_DD_FLAG_RESUME_BIT, &port->dd->dd_flag)) {
  467. mod_timer(&port->cmd_timer,
  468. jiffies + msecs_to_jiffies(30000));
  469. return;
  470. }
  471. for (tag = 0; tag < num_command_slots; tag++) {
  472. /*
  473. * Skip internal command slot as it has
  474. * its own timeout mechanism
  475. */
  476. if (tag == MTIP_TAG_INTERNAL)
  477. continue;
  478. if (atomic_read(&port->commands[tag].active) &&
  479. (time_after(jiffies, port->commands[tag].comp_time))) {
  480. group = tag >> 5;
  481. bit = tag & 0x1F;
  482. command = &port->commands[tag];
  483. fis = (struct host_to_dev_fis *) command->command;
  484. dev_warn(&port->dd->pdev->dev,
  485. "Timeout for command tag %d\n", tag);
  486. cmdto_cnt++;
  487. if (cmdto_cnt == 1)
  488. set_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
  489. /*
  490. * Clear the completed bit. This should prevent
  491. * any interrupt handlers from trying to retire
  492. * the command.
  493. */
  494. writel(1 << bit, port->completed[group]);
  495. /* Call the async completion callback. */
  496. if (likely(command->async_callback))
  497. command->async_callback(command->async_data,
  498. -EIO);
  499. command->async_callback = NULL;
  500. command->comp_func = NULL;
  501. /* Unmap the DMA scatter list entries */
  502. dma_unmap_sg(&port->dd->pdev->dev,
  503. command->sg,
  504. command->scatter_ents,
  505. command->direction);
  506. /*
  507. * Clear the allocated bit and active tag for the
  508. * command.
  509. */
  510. atomic_set(&port->commands[tag].active, 0);
  511. release_slot(port, tag);
  512. up(&port->cmd_slot);
  513. }
  514. }
  515. if (cmdto_cnt) {
  516. dev_warn(&port->dd->pdev->dev,
  517. "%d commands timed out: restarting port",
  518. cmdto_cnt);
  519. mtip_restart_port(port);
  520. clear_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
  521. wake_up_interruptible(&port->svc_wait);
  522. }
  523. /* Restart the timer */
  524. mod_timer(&port->cmd_timer,
  525. jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
  526. }
  527. /*
  528. * IO completion function.
  529. *
  530. * This completion function is called by the driver ISR when a
  531. * command that was issued by the kernel completes. It first calls the
  532. * asynchronous completion function which normally calls back into the block
  533. * layer passing the asynchronous callback data, then unmaps the
  534. * scatter list associated with the completed command, and finally
  535. * clears the allocated bit associated with the completed command.
  536. *
  537. * @port Pointer to the port data structure.
  538. * @tag Tag of the command.
  539. * @data Pointer to driver_data.
  540. * @status Completion status.
  541. *
  542. * return value
  543. * None
  544. */
  545. static void mtip_async_complete(struct mtip_port *port,
  546. int tag,
  547. void *data,
  548. int status)
  549. {
  550. struct mtip_cmd *command;
  551. struct driver_data *dd = data;
  552. int cb_status = status ? -EIO : 0;
  553. if (unlikely(!dd) || unlikely(!port))
  554. return;
  555. command = &port->commands[tag];
  556. if (unlikely(status == PORT_IRQ_TF_ERR)) {
  557. dev_warn(&port->dd->pdev->dev,
  558. "Command tag %d failed due to TFE\n", tag);
  559. }
  560. /* Upper layer callback */
  561. if (likely(command->async_callback))
  562. command->async_callback(command->async_data, cb_status);
  563. command->async_callback = NULL;
  564. command->comp_func = NULL;
  565. /* Unmap the DMA scatter list entries */
  566. dma_unmap_sg(&dd->pdev->dev,
  567. command->sg,
  568. command->scatter_ents,
  569. command->direction);
  570. /* Clear the allocated and active bits for the command */
  571. atomic_set(&port->commands[tag].active, 0);
  572. release_slot(port, tag);
  573. up(&port->cmd_slot);
  574. }
  575. /*
  576. * Internal command completion callback function.
  577. *
  578. * This function is normally called by the driver ISR when an internal
  579. * command completed. This function signals the command completion by
  580. * calling complete().
  581. *
  582. * @port Pointer to the port data structure.
  583. * @tag Tag of the command that has completed.
  584. * @data Pointer to a completion structure.
  585. * @status Completion status.
  586. *
  587. * return value
  588. * None
  589. */
  590. static void mtip_completion(struct mtip_port *port,
  591. int tag,
  592. void *data,
  593. int status)
  594. {
  595. struct mtip_cmd *command = &port->commands[tag];
  596. struct completion *waiting = data;
  597. if (unlikely(status == PORT_IRQ_TF_ERR))
  598. dev_warn(&port->dd->pdev->dev,
  599. "Internal command %d completed with TFE\n", tag);
  600. command->async_callback = NULL;
  601. command->comp_func = NULL;
  602. complete(waiting);
  603. }
  604. /*
  605. * Helper function for tag logging
  606. */
  607. static void print_tags(struct driver_data *dd,
  608. char *msg,
  609. unsigned long *tagbits)
  610. {
  611. unsigned int tag, count = 0;
  612. for (tag = 0; tag < (dd->slot_groups) * 32; tag++) {
  613. if (test_bit(tag, tagbits))
  614. count++;
  615. }
  616. if (count)
  617. dev_info(&dd->pdev->dev, "%s [%i tags]\n", msg, count);
  618. }
  619. /*
  620. * Handle an error.
  621. *
  622. * @dd Pointer to the DRIVER_DATA structure.
  623. *
  624. * return value
  625. * None
  626. */
  627. static void mtip_handle_tfe(struct driver_data *dd)
  628. {
  629. int group, tag, bit, reissue;
  630. struct mtip_port *port;
  631. struct mtip_cmd *command;
  632. u32 completed;
  633. struct host_to_dev_fis *fis;
  634. unsigned long tagaccum[SLOTBITS_IN_LONGS];
  635. dev_warn(&dd->pdev->dev, "Taskfile error\n");
  636. port = dd->port;
  637. /* Stop the timer to prevent command timeouts. */
  638. del_timer(&port->cmd_timer);
  639. /* Set eh_active */
  640. set_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
  641. /* Loop through all the groups */
  642. for (group = 0; group < dd->slot_groups; group++) {
  643. completed = readl(port->completed[group]);
  644. /* clear completed status register in the hardware.*/
  645. writel(completed, port->completed[group]);
  646. /* clear the tag accumulator */
  647. memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
  648. /* Process successfully completed commands */
  649. for (bit = 0; bit < 32 && completed; bit++) {
  650. if (!(completed & (1<<bit)))
  651. continue;
  652. tag = (group << 5) + bit;
  653. /* Skip the internal command slot */
  654. if (tag == MTIP_TAG_INTERNAL)
  655. continue;
  656. command = &port->commands[tag];
  657. if (likely(command->comp_func)) {
  658. set_bit(tag, tagaccum);
  659. atomic_set(&port->commands[tag].active, 0);
  660. command->comp_func(port,
  661. tag,
  662. command->comp_data,
  663. 0);
  664. } else {
  665. dev_err(&port->dd->pdev->dev,
  666. "Missing completion func for tag %d",
  667. tag);
  668. if (mtip_check_surprise_removal(dd->pdev)) {
  669. mtip_command_cleanup(dd);
  670. /* don't proceed further */
  671. return;
  672. }
  673. }
  674. }
  675. }
  676. print_tags(dd, "TFE tags completed:", tagaccum);
  677. /* Restart the port */
  678. mdelay(20);
  679. mtip_restart_port(port);
  680. /* clear the tag accumulator */
  681. memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
  682. /* Loop through all the groups */
  683. for (group = 0; group < dd->slot_groups; group++) {
  684. for (bit = 0; bit < 32; bit++) {
  685. reissue = 1;
  686. tag = (group << 5) + bit;
  687. /* If the active bit is set re-issue the command */
  688. if (atomic_read(&port->commands[tag].active) == 0)
  689. continue;
  690. fis = (struct host_to_dev_fis *)
  691. port->commands[tag].command;
  692. /* Should re-issue? */
  693. if (tag == MTIP_TAG_INTERNAL ||
  694. fis->command == ATA_CMD_SET_FEATURES)
  695. reissue = 0;
  696. /*
  697. * First check if this command has
  698. * exceeded its retries.
  699. */
  700. if (reissue &&
  701. (port->commands[tag].retries-- > 0)) {
  702. set_bit(tag, tagaccum);
  703. /* Re-issue the command. */
  704. mtip_issue_ncq_command(port, tag);
  705. continue;
  706. }
  707. /* Retire a command that will not be reissued */
  708. dev_warn(&port->dd->pdev->dev,
  709. "retiring tag %d\n", tag);
  710. atomic_set(&port->commands[tag].active, 0);
  711. if (port->commands[tag].comp_func)
  712. port->commands[tag].comp_func(
  713. port,
  714. tag,
  715. port->commands[tag].comp_data,
  716. PORT_IRQ_TF_ERR);
  717. else
  718. dev_warn(&port->dd->pdev->dev,
  719. "Bad completion for tag %d\n",
  720. tag);
  721. }
  722. }
  723. print_tags(dd, "TFE tags reissued:", tagaccum);
  724. /* clear eh_active */
  725. clear_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
  726. wake_up_interruptible(&port->svc_wait);
  727. mod_timer(&port->cmd_timer,
  728. jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
  729. }
  730. /*
  731. * Handle a set device bits interrupt
  732. */
  733. static inline void mtip_process_sdbf(struct driver_data *dd)
  734. {
  735. struct mtip_port *port = dd->port;
  736. int group, tag, bit;
  737. u32 completed;
  738. struct mtip_cmd *command;
  739. /* walk all bits in all slot groups */
  740. for (group = 0; group < dd->slot_groups; group++) {
  741. completed = readl(port->completed[group]);
  742. /* clear completed status register in the hardware.*/
  743. writel(completed, port->completed[group]);
  744. /* Process completed commands. */
  745. for (bit = 0;
  746. (bit < 32) && completed;
  747. bit++, completed >>= 1) {
  748. if (completed & 0x01) {
  749. tag = (group << 5) | bit;
  750. /* skip internal command slot. */
  751. if (unlikely(tag == MTIP_TAG_INTERNAL))
  752. continue;
  753. command = &port->commands[tag];
  754. /* make internal callback */
  755. if (likely(command->comp_func)) {
  756. command->comp_func(
  757. port,
  758. tag,
  759. command->comp_data,
  760. 0);
  761. } else {
  762. dev_warn(&dd->pdev->dev,
  763. "Null completion "
  764. "for tag %d",
  765. tag);
  766. if (mtip_check_surprise_removal(
  767. dd->pdev)) {
  768. mtip_command_cleanup(dd);
  769. return;
  770. }
  771. }
  772. }
  773. }
  774. }
  775. }
  776. /*
  777. * Process legacy pio and d2h interrupts
  778. */
  779. static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
  780. {
  781. struct mtip_port *port = dd->port;
  782. struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
  783. if (test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) &&
  784. (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  785. & (1 << MTIP_TAG_INTERNAL))) {
  786. if (cmd->comp_func) {
  787. cmd->comp_func(port,
  788. MTIP_TAG_INTERNAL,
  789. cmd->comp_data,
  790. 0);
  791. return;
  792. }
  793. }
  794. dev_warn(&dd->pdev->dev, "IRQ status 0x%x ignored.\n", port_stat);
  795. return;
  796. }
  797. /*
  798. * Demux and handle errors
  799. */
  800. static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
  801. {
  802. if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
  803. mtip_handle_tfe(dd);
  804. if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
  805. dev_warn(&dd->pdev->dev,
  806. "Clearing PxSERR.DIAG.x\n");
  807. writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
  808. }
  809. if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
  810. dev_warn(&dd->pdev->dev,
  811. "Clearing PxSERR.DIAG.n\n");
  812. writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
  813. }
  814. if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
  815. dev_warn(&dd->pdev->dev,
  816. "Port stat errors %x unhandled\n",
  817. (port_stat & ~PORT_IRQ_HANDLED));
  818. }
  819. }
  820. static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
  821. {
  822. struct driver_data *dd = (struct driver_data *) data;
  823. struct mtip_port *port = dd->port;
  824. u32 hba_stat, port_stat;
  825. int rv = IRQ_NONE;
  826. hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
  827. if (hba_stat) {
  828. rv = IRQ_HANDLED;
  829. /* Acknowledge the interrupt status on the port.*/
  830. port_stat = readl(port->mmio + PORT_IRQ_STAT);
  831. writel(port_stat, port->mmio + PORT_IRQ_STAT);
  832. /* Demux port status */
  833. if (likely(port_stat & PORT_IRQ_SDB_FIS))
  834. mtip_process_sdbf(dd);
  835. if (unlikely(port_stat & PORT_IRQ_ERR)) {
  836. if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
  837. mtip_command_cleanup(dd);
  838. /* don't proceed further */
  839. return IRQ_HANDLED;
  840. }
  841. if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
  842. &dd->dd_flag))
  843. return rv;
  844. mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
  845. }
  846. if (unlikely(port_stat & PORT_IRQ_LEGACY))
  847. mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
  848. }
  849. /* acknowledge interrupt */
  850. writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
  851. return rv;
  852. }
  853. /*
  854. * Wrapper for mtip_handle_irq
  855. * (ignores return code)
  856. */
  857. static void mtip_tasklet(unsigned long data)
  858. {
  859. mtip_handle_irq((struct driver_data *) data);
  860. }
  861. /*
  862. * HBA interrupt subroutine.
  863. *
  864. * @irq IRQ number.
  865. * @instance Pointer to the driver data structure.
  866. *
  867. * return value
  868. * IRQ_HANDLED A HBA interrupt was pending and handled.
  869. * IRQ_NONE This interrupt was not for the HBA.
  870. */
  871. static irqreturn_t mtip_irq_handler(int irq, void *instance)
  872. {
  873. struct driver_data *dd = instance;
  874. tasklet_schedule(&dd->tasklet);
  875. return IRQ_HANDLED;
  876. }
  877. static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
  878. {
  879. atomic_set(&port->commands[tag].active, 1);
  880. writel(1 << MTIP_TAG_BIT(tag),
  881. port->cmd_issue[MTIP_TAG_INDEX(tag)]);
  882. }
  883. /*
  884. * Wait for port to quiesce
  885. *
  886. * @port Pointer to port data structure
  887. * @timeout Max duration to wait (ms)
  888. *
  889. * return value
  890. * 0 Success
  891. * -EBUSY Commands still active
  892. */
  893. static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
  894. {
  895. unsigned long to;
  896. unsigned int n;
  897. unsigned int active = 1;
  898. to = jiffies + msecs_to_jiffies(timeout);
  899. do {
  900. if (test_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags) &&
  901. test_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags)) {
  902. msleep(20);
  903. continue; /* svc thd is actively issuing commands */
  904. }
  905. if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
  906. &port->dd->dd_flag))
  907. return -EFAULT;
  908. /*
  909. * Ignore s_active bit 0 of array element 0.
  910. * This bit will always be set
  911. */
  912. active = readl(port->s_active[0]) & 0xFFFFFFFE;
  913. for (n = 1; n < port->dd->slot_groups; n++)
  914. active |= readl(port->s_active[n]);
  915. if (!active)
  916. break;
  917. msleep(20);
  918. } while (time_before(jiffies, to));
  919. return active ? -EBUSY : 0;
  920. }
  921. /*
  922. * Execute an internal command and wait for the completion.
  923. *
  924. * @port Pointer to the port data structure.
  925. * @fis Pointer to the FIS that describes the command.
  926. * @fis_len Length in WORDS of the FIS.
  927. * @buffer DMA accessible for command data.
  928. * @buf_len Length, in bytes, of the data buffer.
  929. * @opts Command header options, excluding the FIS length
  930. * and the number of PRD entries.
  931. * @timeout Time in ms to wait for the command to complete.
  932. *
  933. * return value
  934. * 0 Command completed successfully.
  935. * -EFAULT The buffer address is not correctly aligned.
  936. * -EBUSY Internal command or other IO in progress.
  937. * -EAGAIN Time out waiting for command to complete.
  938. */
  939. static int mtip_exec_internal_command(struct mtip_port *port,
  940. void *fis,
  941. int fis_len,
  942. dma_addr_t buffer,
  943. int buf_len,
  944. u32 opts,
  945. gfp_t atomic,
  946. unsigned long timeout)
  947. {
  948. struct mtip_cmd_sg *command_sg;
  949. DECLARE_COMPLETION_ONSTACK(wait);
  950. int rv = 0;
  951. struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
  952. /* Make sure the buffer is 8 byte aligned. This is asic specific. */
  953. if (buffer & 0x00000007) {
  954. dev_err(&port->dd->pdev->dev,
  955. "SG buffer is not 8 byte aligned\n");
  956. return -EFAULT;
  957. }
  958. /* Only one internal command should be running at a time */
  959. if (test_and_set_bit(MTIP_TAG_INTERNAL, port->allocated)) {
  960. dev_warn(&port->dd->pdev->dev,
  961. "Internal command already active\n");
  962. return -EBUSY;
  963. }
  964. set_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
  965. if (atomic == GFP_KERNEL) {
  966. /* wait for io to complete if non atomic */
  967. if (mtip_quiesce_io(port, 5000) < 0) {
  968. dev_warn(&port->dd->pdev->dev,
  969. "Failed to quiesce IO\n");
  970. release_slot(port, MTIP_TAG_INTERNAL);
  971. clear_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
  972. wake_up_interruptible(&port->svc_wait);
  973. return -EBUSY;
  974. }
  975. /* Set the completion function and data for the command. */
  976. int_cmd->comp_data = &wait;
  977. int_cmd->comp_func = mtip_completion;
  978. } else {
  979. /* Clear completion - we're going to poll */
  980. int_cmd->comp_data = NULL;
  981. int_cmd->comp_func = NULL;
  982. }
  983. /* Copy the command to the command table */
  984. memcpy(int_cmd->command, fis, fis_len*4);
  985. /* Populate the SG list */
  986. int_cmd->command_header->opts =
  987. __force_bit2int cpu_to_le32(opts | fis_len);
  988. if (buf_len) {
  989. command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
  990. command_sg->info =
  991. __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
  992. command_sg->dba =
  993. __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
  994. command_sg->dba_upper =
  995. __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
  996. int_cmd->command_header->opts |=
  997. __force_bit2int cpu_to_le32((1 << 16));
  998. }
  999. /* Populate the command header */
  1000. int_cmd->command_header->byte_count = 0;
  1001. /* Issue the command to the hardware */
  1002. mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
  1003. /* Poll if atomic, wait_for_completion otherwise */
  1004. if (atomic == GFP_KERNEL) {
  1005. /* Wait for the command to complete or timeout. */
  1006. if (wait_for_completion_timeout(
  1007. &wait,
  1008. msecs_to_jiffies(timeout)) == 0) {
  1009. dev_err(&port->dd->pdev->dev,
  1010. "Internal command did not complete [%d] "
  1011. "within timeout of %lu ms\n",
  1012. atomic, timeout);
  1013. if (mtip_check_surprise_removal(port->dd->pdev) ||
  1014. test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
  1015. &port->dd->dd_flag)) {
  1016. rv = -ENXIO;
  1017. goto exec_ic_exit;
  1018. }
  1019. rv = -EAGAIN;
  1020. }
  1021. if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  1022. & (1 << MTIP_TAG_INTERNAL)) {
  1023. dev_warn(&port->dd->pdev->dev,
  1024. "Retiring internal command but CI is 1.\n");
  1025. if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
  1026. &port->dd->dd_flag)) {
  1027. hba_reset_nosleep(port->dd);
  1028. rv = -ENXIO;
  1029. } else {
  1030. mtip_restart_port(port);
  1031. rv = -EAGAIN;
  1032. }
  1033. goto exec_ic_exit;
  1034. }
  1035. } else {
  1036. /* Spin for <timeout> checking if command still outstanding */
  1037. timeout = jiffies + msecs_to_jiffies(timeout);
  1038. while ((readl(
  1039. port->cmd_issue[MTIP_TAG_INTERNAL])
  1040. & (1 << MTIP_TAG_INTERNAL))
  1041. && time_before(jiffies, timeout)) {
  1042. if (mtip_check_surprise_removal(port->dd->pdev) ||
  1043. test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
  1044. &port->dd->dd_flag)) {
  1045. rv = -ENXIO;
  1046. goto exec_ic_exit;
  1047. }
  1048. }
  1049. if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  1050. & (1 << MTIP_TAG_INTERNAL)) {
  1051. dev_err(&port->dd->pdev->dev,
  1052. "Internal command did not complete [%d]\n",
  1053. atomic);
  1054. rv = -EAGAIN;
  1055. if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
  1056. &port->dd->dd_flag)) {
  1057. hba_reset_nosleep(port->dd);
  1058. rv = -ENXIO;
  1059. } else {
  1060. mtip_restart_port(port);
  1061. rv = -EAGAIN;
  1062. }
  1063. }
  1064. }
  1065. exec_ic_exit:
  1066. /* Clear the allocated and active bits for the internal command. */
  1067. atomic_set(&int_cmd->active, 0);
  1068. release_slot(port, MTIP_TAG_INTERNAL);
  1069. clear_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
  1070. wake_up_interruptible(&port->svc_wait);
  1071. return rv;
  1072. }
  1073. /*
  1074. * Byte-swap ATA ID strings.
  1075. *
  1076. * ATA identify data contains strings in byte-swapped 16-bit words.
  1077. * They must be swapped (on all architectures) to be usable as C strings.
  1078. * This function swaps bytes in-place.
  1079. *
  1080. * @buf The buffer location of the string
  1081. * @len The number of bytes to swap
  1082. *
  1083. * return value
  1084. * None
  1085. */
  1086. static inline void ata_swap_string(u16 *buf, unsigned int len)
  1087. {
  1088. int i;
  1089. for (i = 0; i < (len/2); i++)
  1090. be16_to_cpus(&buf[i]);
  1091. }
  1092. /*
  1093. * Request the device identity information.
  1094. *
  1095. * If a user space buffer is not specified, i.e. is NULL, the
  1096. * identify information is still read from the drive and placed
  1097. * into the identify data buffer (@e port->identify) in the
  1098. * port data structure.
  1099. * When the identify buffer contains valid identify information @e
  1100. * port->identify_valid is non-zero.
  1101. *
  1102. * @port Pointer to the port structure.
  1103. * @user_buffer A user space buffer where the identify data should be
  1104. * copied.
  1105. *
  1106. * return value
  1107. * 0 Command completed successfully.
  1108. * -EFAULT An error occurred while coping data to the user buffer.
  1109. * -1 Command failed.
  1110. */
  1111. static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
  1112. {
  1113. int rv = 0;
  1114. struct host_to_dev_fis fis;
  1115. if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  1116. return -EFAULT;
  1117. /* Build the FIS. */
  1118. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1119. fis.type = 0x27;
  1120. fis.opts = 1 << 7;
  1121. fis.command = ATA_CMD_ID_ATA;
  1122. /* Set the identify information as invalid. */
  1123. port->identify_valid = 0;
  1124. /* Clear the identify information. */
  1125. memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
  1126. /* Execute the command. */
  1127. if (mtip_exec_internal_command(port,
  1128. &fis,
  1129. 5,
  1130. port->identify_dma,
  1131. sizeof(u16) * ATA_ID_WORDS,
  1132. 0,
  1133. GFP_KERNEL,
  1134. MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
  1135. < 0) {
  1136. rv = -1;
  1137. goto out;
  1138. }
  1139. /*
  1140. * Perform any necessary byte-swapping. Yes, the kernel does in fact
  1141. * perform field-sensitive swapping on the string fields.
  1142. * See the kernel use of ata_id_string() for proof of this.
  1143. */
  1144. #ifdef __LITTLE_ENDIAN
  1145. ata_swap_string(port->identify + 27, 40); /* model string*/
  1146. ata_swap_string(port->identify + 23, 8); /* firmware string*/
  1147. ata_swap_string(port->identify + 10, 20); /* serial# string*/
  1148. #else
  1149. {
  1150. int i;
  1151. for (i = 0; i < ATA_ID_WORDS; i++)
  1152. port->identify[i] = le16_to_cpu(port->identify[i]);
  1153. }
  1154. #endif
  1155. /* Set the identify buffer as valid. */
  1156. port->identify_valid = 1;
  1157. if (user_buffer) {
  1158. if (copy_to_user(
  1159. user_buffer,
  1160. port->identify,
  1161. ATA_ID_WORDS * sizeof(u16))) {
  1162. rv = -EFAULT;
  1163. goto out;
  1164. }
  1165. }
  1166. out:
  1167. return rv;
  1168. }
  1169. /*
  1170. * Issue a standby immediate command to the device.
  1171. *
  1172. * @port Pointer to the port structure.
  1173. *
  1174. * return value
  1175. * 0 Command was executed successfully.
  1176. * -1 An error occurred while executing the command.
  1177. */
  1178. static int mtip_standby_immediate(struct mtip_port *port)
  1179. {
  1180. int rv;
  1181. struct host_to_dev_fis fis;
  1182. /* Build the FIS. */
  1183. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1184. fis.type = 0x27;
  1185. fis.opts = 1 << 7;
  1186. fis.command = ATA_CMD_STANDBYNOW1;
  1187. /* Execute the command. Use a 15-second timeout for large drives. */
  1188. rv = mtip_exec_internal_command(port,
  1189. &fis,
  1190. 5,
  1191. 0,
  1192. 0,
  1193. 0,
  1194. GFP_KERNEL,
  1195. 15000);
  1196. return rv;
  1197. }
  1198. /*
  1199. * Get the drive capacity.
  1200. *
  1201. * @dd Pointer to the device data structure.
  1202. * @sectors Pointer to the variable that will receive the sector count.
  1203. *
  1204. * return value
  1205. * 1 Capacity was returned successfully.
  1206. * 0 The identify information is invalid.
  1207. */
  1208. static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
  1209. {
  1210. struct mtip_port *port = dd->port;
  1211. u64 total, raw0, raw1, raw2, raw3;
  1212. raw0 = port->identify[100];
  1213. raw1 = port->identify[101];
  1214. raw2 = port->identify[102];
  1215. raw3 = port->identify[103];
  1216. total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
  1217. *sectors = total;
  1218. return (bool) !!port->identify_valid;
  1219. }
  1220. /*
  1221. * Reset the HBA.
  1222. *
  1223. * Resets the HBA by setting the HBA Reset bit in the Global
  1224. * HBA Control register. After setting the HBA Reset bit the
  1225. * function waits for 1 second before reading the HBA Reset
  1226. * bit to make sure it has cleared. If HBA Reset is not clear
  1227. * an error is returned. Cannot be used in non-blockable
  1228. * context.
  1229. *
  1230. * @dd Pointer to the driver data structure.
  1231. *
  1232. * return value
  1233. * 0 The reset was successful.
  1234. * -1 The HBA Reset bit did not clear.
  1235. */
  1236. static int mtip_hba_reset(struct driver_data *dd)
  1237. {
  1238. mtip_deinit_port(dd->port);
  1239. /* Set the reset bit */
  1240. writel(HOST_RESET, dd->mmio + HOST_CTL);
  1241. /* Flush */
  1242. readl(dd->mmio + HOST_CTL);
  1243. /* Wait for reset to clear */
  1244. ssleep(1);
  1245. /* Check the bit has cleared */
  1246. if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
  1247. dev_err(&dd->pdev->dev,
  1248. "Reset bit did not clear.\n");
  1249. return -1;
  1250. }
  1251. return 0;
  1252. }
  1253. /*
  1254. * Display the identify command data.
  1255. *
  1256. * @port Pointer to the port data structure.
  1257. *
  1258. * return value
  1259. * None
  1260. */
  1261. static void mtip_dump_identify(struct mtip_port *port)
  1262. {
  1263. sector_t sectors;
  1264. unsigned short revid;
  1265. char cbuf[42];
  1266. if (!port->identify_valid)
  1267. return;
  1268. strlcpy(cbuf, (char *)(port->identify+10), 21);
  1269. dev_info(&port->dd->pdev->dev,
  1270. "Serial No.: %s\n", cbuf);
  1271. strlcpy(cbuf, (char *)(port->identify+23), 9);
  1272. dev_info(&port->dd->pdev->dev,
  1273. "Firmware Ver.: %s\n", cbuf);
  1274. strlcpy(cbuf, (char *)(port->identify+27), 41);
  1275. dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
  1276. if (mtip_hw_get_capacity(port->dd, &sectors))
  1277. dev_info(&port->dd->pdev->dev,
  1278. "Capacity: %llu sectors (%llu MB)\n",
  1279. (u64)sectors,
  1280. ((u64)sectors) * ATA_SECT_SIZE >> 20);
  1281. pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
  1282. switch (revid & 0xFF) {
  1283. case 0x1:
  1284. strlcpy(cbuf, "A0", 3);
  1285. break;
  1286. case 0x3:
  1287. strlcpy(cbuf, "A2", 3);
  1288. break;
  1289. default:
  1290. strlcpy(cbuf, "?", 2);
  1291. break;
  1292. }
  1293. dev_info(&port->dd->pdev->dev,
  1294. "Card Type: %s\n", cbuf);
  1295. }
  1296. /*
  1297. * Map the commands scatter list into the command table.
  1298. *
  1299. * @command Pointer to the command.
  1300. * @nents Number of scatter list entries.
  1301. *
  1302. * return value
  1303. * None
  1304. */
  1305. static inline void fill_command_sg(struct driver_data *dd,
  1306. struct mtip_cmd *command,
  1307. int nents)
  1308. {
  1309. int n;
  1310. unsigned int dma_len;
  1311. struct mtip_cmd_sg *command_sg;
  1312. struct scatterlist *sg = command->sg;
  1313. command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
  1314. for (n = 0; n < nents; n++) {
  1315. dma_len = sg_dma_len(sg);
  1316. if (dma_len > 0x400000)
  1317. dev_err(&dd->pdev->dev,
  1318. "DMA segment length truncated\n");
  1319. command_sg->info = __force_bit2int
  1320. cpu_to_le32((dma_len-1) & 0x3FFFFF);
  1321. command_sg->dba = __force_bit2int
  1322. cpu_to_le32(sg_dma_address(sg));
  1323. command_sg->dba_upper = __force_bit2int
  1324. cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
  1325. command_sg++;
  1326. sg++;
  1327. }
  1328. }
  1329. /*
  1330. * @brief Execute a drive command.
  1331. *
  1332. * return value 0 The command completed successfully.
  1333. * return value -1 An error occurred while executing the command.
  1334. */
  1335. static int exec_drive_task(struct mtip_port *port, u8 *command)
  1336. {
  1337. struct host_to_dev_fis fis;
  1338. struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
  1339. /* Build the FIS. */
  1340. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1341. fis.type = 0x27;
  1342. fis.opts = 1 << 7;
  1343. fis.command = command[0];
  1344. fis.features = command[1];
  1345. fis.sect_count = command[2];
  1346. fis.sector = command[3];
  1347. fis.cyl_low = command[4];
  1348. fis.cyl_hi = command[5];
  1349. fis.device = command[6] & ~0x10; /* Clear the dev bit*/
  1350. dbg_printk(MTIP_DRV_NAME "%s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
  1351. __func__,
  1352. command[0],
  1353. command[1],
  1354. command[2],
  1355. command[3],
  1356. command[4],
  1357. command[5],
  1358. command[6]);
  1359. /* Execute the command. */
  1360. if (mtip_exec_internal_command(port,
  1361. &fis,
  1362. 5,
  1363. 0,
  1364. 0,
  1365. 0,
  1366. GFP_KERNEL,
  1367. MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
  1368. return -1;
  1369. }
  1370. command[0] = reply->command; /* Status*/
  1371. command[1] = reply->features; /* Error*/
  1372. command[4] = reply->cyl_low;
  1373. command[5] = reply->cyl_hi;
  1374. dbg_printk(MTIP_DRV_NAME "%s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
  1375. __func__,
  1376. command[0],
  1377. command[1],
  1378. command[4],
  1379. command[5]);
  1380. return 0;
  1381. }
  1382. /*
  1383. * @brief Execute a drive command.
  1384. *
  1385. * @param port Pointer to the port data structure.
  1386. * @param command Pointer to the user specified command parameters.
  1387. * @param user_buffer Pointer to the user space buffer where read sector
  1388. * data should be copied.
  1389. *
  1390. * return value 0 The command completed successfully.
  1391. * return value -EFAULT An error occurred while copying the completion
  1392. * data to the user space buffer.
  1393. * return value -1 An error occurred while executing the command.
  1394. */
  1395. static int exec_drive_command(struct mtip_port *port, u8 *command,
  1396. void __user *user_buffer)
  1397. {
  1398. struct host_to_dev_fis fis;
  1399. struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
  1400. /* Build the FIS. */
  1401. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1402. fis.type = 0x27;
  1403. fis.opts = 1 << 7;
  1404. fis.command = command[0];
  1405. fis.features = command[2];
  1406. fis.sect_count = command[3];
  1407. if (fis.command == ATA_CMD_SMART) {
  1408. fis.sector = command[1];
  1409. fis.cyl_low = 0x4F;
  1410. fis.cyl_hi = 0xC2;
  1411. }
  1412. dbg_printk(MTIP_DRV_NAME
  1413. "%s: User Command: cmd %x, sect %x, "
  1414. "feat %x, sectcnt %x\n",
  1415. __func__,
  1416. command[0],
  1417. command[1],
  1418. command[2],
  1419. command[3]);
  1420. memset(port->sector_buffer, 0x00, ATA_SECT_SIZE);
  1421. /* Execute the command. */
  1422. if (mtip_exec_internal_command(port,
  1423. &fis,
  1424. 5,
  1425. port->sector_buffer_dma,
  1426. (command[3] != 0) ? ATA_SECT_SIZE : 0,
  1427. 0,
  1428. GFP_KERNEL,
  1429. MTIP_IOCTL_COMMAND_TIMEOUT_MS)
  1430. < 0) {
  1431. return -1;
  1432. }
  1433. /* Collect the completion status. */
  1434. command[0] = reply->command; /* Status*/
  1435. command[1] = reply->features; /* Error*/
  1436. command[2] = command[3];
  1437. dbg_printk(MTIP_DRV_NAME
  1438. "%s: Completion Status: stat %x, "
  1439. "err %x, cmd %x\n",
  1440. __func__,
  1441. command[0],
  1442. command[1],
  1443. command[2]);
  1444. if (user_buffer && command[3]) {
  1445. if (copy_to_user(user_buffer,
  1446. port->sector_buffer,
  1447. ATA_SECT_SIZE * command[3])) {
  1448. return -EFAULT;
  1449. }
  1450. }
  1451. return 0;
  1452. }
  1453. /*
  1454. * Indicates whether a command has a single sector payload.
  1455. *
  1456. * @command passed to the device to perform the certain event.
  1457. * @features passed to the device to perform the certain event.
  1458. *
  1459. * return value
  1460. * 1 command is one that always has a single sector payload,
  1461. * regardless of the value in the Sector Count field.
  1462. * 0 otherwise
  1463. *
  1464. */
  1465. static unsigned int implicit_sector(unsigned char command,
  1466. unsigned char features)
  1467. {
  1468. unsigned int rv = 0;
  1469. /* list of commands that have an implicit sector count of 1 */
  1470. switch (command) {
  1471. case ATA_CMD_SEC_SET_PASS:
  1472. case ATA_CMD_SEC_UNLOCK:
  1473. case ATA_CMD_SEC_ERASE_PREP:
  1474. case ATA_CMD_SEC_ERASE_UNIT:
  1475. case ATA_CMD_SEC_FREEZE_LOCK:
  1476. case ATA_CMD_SEC_DISABLE_PASS:
  1477. case ATA_CMD_PMP_READ:
  1478. case ATA_CMD_PMP_WRITE:
  1479. rv = 1;
  1480. break;
  1481. case ATA_CMD_SET_MAX:
  1482. if (features == ATA_SET_MAX_UNLOCK)
  1483. rv = 1;
  1484. break;
  1485. case ATA_CMD_SMART:
  1486. if ((features == ATA_SMART_READ_VALUES) ||
  1487. (features == ATA_SMART_READ_THRESHOLDS))
  1488. rv = 1;
  1489. break;
  1490. case ATA_CMD_CONF_OVERLAY:
  1491. if ((features == ATA_DCO_IDENTIFY) ||
  1492. (features == ATA_DCO_SET))
  1493. rv = 1;
  1494. break;
  1495. }
  1496. return rv;
  1497. }
  1498. /*
  1499. * Executes a taskfile
  1500. * See ide_taskfile_ioctl() for derivation
  1501. */
  1502. static int exec_drive_taskfile(struct driver_data *dd,
  1503. void __user *buf,
  1504. ide_task_request_t *req_task,
  1505. int outtotal)
  1506. {
  1507. struct host_to_dev_fis fis;
  1508. struct host_to_dev_fis *reply;
  1509. u8 *outbuf = NULL;
  1510. u8 *inbuf = NULL;
  1511. dma_addr_t outbuf_dma = 0;
  1512. dma_addr_t inbuf_dma = 0;
  1513. dma_addr_t dma_buffer = 0;
  1514. int err = 0;
  1515. unsigned int taskin = 0;
  1516. unsigned int taskout = 0;
  1517. u8 nsect = 0;
  1518. unsigned int timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
  1519. unsigned int force_single_sector;
  1520. unsigned int transfer_size;
  1521. unsigned long task_file_data;
  1522. int intotal = outtotal + req_task->out_size;
  1523. taskout = req_task->out_size;
  1524. taskin = req_task->in_size;
  1525. /* 130560 = 512 * 0xFF*/
  1526. if (taskin > 130560 || taskout > 130560) {
  1527. err = -EINVAL;
  1528. goto abort;
  1529. }
  1530. if (taskout) {
  1531. outbuf = kzalloc(taskout, GFP_KERNEL);
  1532. if (outbuf == NULL) {
  1533. err = -ENOMEM;
  1534. goto abort;
  1535. }
  1536. if (copy_from_user(outbuf, buf + outtotal, taskout)) {
  1537. err = -EFAULT;
  1538. goto abort;
  1539. }
  1540. outbuf_dma = pci_map_single(dd->pdev,
  1541. outbuf,
  1542. taskout,
  1543. DMA_TO_DEVICE);
  1544. if (outbuf_dma == 0) {
  1545. err = -ENOMEM;
  1546. goto abort;
  1547. }
  1548. dma_buffer = outbuf_dma;
  1549. }
  1550. if (taskin) {
  1551. inbuf = kzalloc(taskin, GFP_KERNEL);
  1552. if (inbuf == NULL) {
  1553. err = -ENOMEM;
  1554. goto abort;
  1555. }
  1556. if (copy_from_user(inbuf, buf + intotal, taskin)) {
  1557. err = -EFAULT;
  1558. goto abort;
  1559. }
  1560. inbuf_dma = pci_map_single(dd->pdev,
  1561. inbuf,
  1562. taskin, DMA_FROM_DEVICE);
  1563. if (inbuf_dma == 0) {
  1564. err = -ENOMEM;
  1565. goto abort;
  1566. }
  1567. dma_buffer = inbuf_dma;
  1568. }
  1569. /* only supports PIO and non-data commands from this ioctl. */
  1570. switch (req_task->data_phase) {
  1571. case TASKFILE_OUT:
  1572. nsect = taskout / ATA_SECT_SIZE;
  1573. reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
  1574. break;
  1575. case TASKFILE_IN:
  1576. reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
  1577. break;
  1578. case TASKFILE_NO_DATA:
  1579. reply = (dd->port->rxfis + RX_FIS_D2H_REG);
  1580. break;
  1581. default:
  1582. err = -EINVAL;
  1583. goto abort;
  1584. }
  1585. /* Build the FIS. */
  1586. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1587. fis.type = 0x27;
  1588. fis.opts = 1 << 7;
  1589. fis.command = req_task->io_ports[7];
  1590. fis.features = req_task->io_ports[1];
  1591. fis.sect_count = req_task->io_ports[2];
  1592. fis.lba_low = req_task->io_ports[3];
  1593. fis.lba_mid = req_task->io_ports[4];
  1594. fis.lba_hi = req_task->io_ports[5];
  1595. /* Clear the dev bit*/
  1596. fis.device = req_task->io_ports[6] & ~0x10;
  1597. if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
  1598. req_task->in_flags.all =
  1599. IDE_TASKFILE_STD_IN_FLAGS |
  1600. (IDE_HOB_STD_IN_FLAGS << 8);
  1601. fis.lba_low_ex = req_task->hob_ports[3];
  1602. fis.lba_mid_ex = req_task->hob_ports[4];
  1603. fis.lba_hi_ex = req_task->hob_ports[5];
  1604. fis.features_ex = req_task->hob_ports[1];
  1605. fis.sect_cnt_ex = req_task->hob_ports[2];
  1606. } else {
  1607. req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
  1608. }
  1609. force_single_sector = implicit_sector(fis.command, fis.features);
  1610. if ((taskin || taskout) && (!fis.sect_count)) {
  1611. if (nsect)
  1612. fis.sect_count = nsect;
  1613. else {
  1614. if (!force_single_sector) {
  1615. dev_warn(&dd->pdev->dev,
  1616. "data movement but "
  1617. "sect_count is 0\n");
  1618. err = -EINVAL;
  1619. goto abort;
  1620. }
  1621. }
  1622. }
  1623. dbg_printk(MTIP_DRV_NAME
  1624. "taskfile: cmd %x, feat %x, nsect %x,"
  1625. " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
  1626. " head/dev %x\n",
  1627. fis.command,
  1628. fis.features,
  1629. fis.sect_count,
  1630. fis.lba_low,
  1631. fis.lba_mid,
  1632. fis.lba_hi,
  1633. fis.device);
  1634. switch (fis.command) {
  1635. case ATA_CMD_DOWNLOAD_MICRO:
  1636. /* Change timeout for Download Microcode to 60 seconds.*/
  1637. timeout = 60000;
  1638. break;
  1639. case ATA_CMD_SEC_ERASE_UNIT:
  1640. /* Change timeout for Security Erase Unit to 4 minutes.*/
  1641. timeout = 240000;
  1642. break;
  1643. case ATA_CMD_STANDBYNOW1:
  1644. /* Change timeout for standby immediate to 10 seconds.*/
  1645. timeout = 10000;
  1646. break;
  1647. case 0xF7:
  1648. case 0xFA:
  1649. /* Change timeout for vendor unique command to 10 secs */
  1650. timeout = 10000;
  1651. break;
  1652. case ATA_CMD_SMART:
  1653. /* Change timeout for vendor unique command to 10 secs */
  1654. timeout = 10000;
  1655. break;
  1656. default:
  1657. timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
  1658. break;
  1659. }
  1660. /* Determine the correct transfer size.*/
  1661. if (force_single_sector)
  1662. transfer_size = ATA_SECT_SIZE;
  1663. else
  1664. transfer_size = ATA_SECT_SIZE * fis.sect_count;
  1665. /* Execute the command.*/
  1666. if (mtip_exec_internal_command(dd->port,
  1667. &fis,
  1668. 5,
  1669. dma_buffer,
  1670. transfer_size,
  1671. 0,
  1672. GFP_KERNEL,
  1673. timeout) < 0) {
  1674. err = -EIO;
  1675. goto abort;
  1676. }
  1677. task_file_data = readl(dd->port->mmio+PORT_TFDATA);
  1678. if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
  1679. reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
  1680. req_task->io_ports[7] = reply->control;
  1681. } else {
  1682. reply = dd->port->rxfis + RX_FIS_D2H_REG;
  1683. req_task->io_ports[7] = reply->command;
  1684. }
  1685. /* reclaim the DMA buffers.*/
  1686. if (inbuf_dma)
  1687. pci_unmap_single(dd->pdev, inbuf_dma,
  1688. taskin, DMA_FROM_DEVICE);
  1689. if (outbuf_dma)
  1690. pci_unmap_single(dd->pdev, outbuf_dma,
  1691. taskout, DMA_TO_DEVICE);
  1692. inbuf_dma = 0;
  1693. outbuf_dma = 0;
  1694. /* return the ATA registers to the caller.*/
  1695. req_task->io_ports[1] = reply->features;
  1696. req_task->io_ports[2] = reply->sect_count;
  1697. req_task->io_ports[3] = reply->lba_low;
  1698. req_task->io_ports[4] = reply->lba_mid;
  1699. req_task->io_ports[5] = reply->lba_hi;
  1700. req_task->io_ports[6] = reply->device;
  1701. if (req_task->out_flags.all & 1) {
  1702. req_task->hob_ports[3] = reply->lba_low_ex;
  1703. req_task->hob_ports[4] = reply->lba_mid_ex;
  1704. req_task->hob_ports[5] = reply->lba_hi_ex;
  1705. req_task->hob_ports[1] = reply->features_ex;
  1706. req_task->hob_ports[2] = reply->sect_cnt_ex;
  1707. }
  1708. /* Com rest after secure erase or lowlevel format */
  1709. if (((fis.command == ATA_CMD_SEC_ERASE_UNIT) ||
  1710. ((fis.command == 0xFC) &&
  1711. (fis.features == 0x27 || fis.features == 0x72 ||
  1712. fis.features == 0x62 || fis.features == 0x26))) &&
  1713. !(reply->command & 1)) {
  1714. mtip_restart_port(dd->port);
  1715. }
  1716. dbg_printk(MTIP_DRV_NAME
  1717. "%s: Completion: stat %x,"
  1718. "err %x, sect_cnt %x, lbalo %x,"
  1719. "lbamid %x, lbahi %x, dev %x\n",
  1720. __func__,
  1721. req_task->io_ports[7],
  1722. req_task->io_ports[1],
  1723. req_task->io_ports[2],
  1724. req_task->io_ports[3],
  1725. req_task->io_ports[4],
  1726. req_task->io_ports[5],
  1727. req_task->io_ports[6]);
  1728. if (taskout) {
  1729. if (copy_to_user(buf + outtotal, outbuf, taskout)) {
  1730. err = -EFAULT;
  1731. goto abort;
  1732. }
  1733. }
  1734. if (taskin) {
  1735. if (copy_to_user(buf + intotal, inbuf, taskin)) {
  1736. err = -EFAULT;
  1737. goto abort;
  1738. }
  1739. }
  1740. abort:
  1741. if (inbuf_dma)
  1742. pci_unmap_single(dd->pdev, inbuf_dma,
  1743. taskin, DMA_FROM_DEVICE);
  1744. if (outbuf_dma)
  1745. pci_unmap_single(dd->pdev, outbuf_dma,
  1746. taskout, DMA_TO_DEVICE);
  1747. kfree(outbuf);
  1748. kfree(inbuf);
  1749. return err;
  1750. }
  1751. /*
  1752. * Handle IOCTL calls from the Block Layer.
  1753. *
  1754. * This function is called by the Block Layer when it receives an IOCTL
  1755. * command that it does not understand. If the IOCTL command is not supported
  1756. * this function returns -ENOTTY.
  1757. *
  1758. * @dd Pointer to the driver data structure.
  1759. * @cmd IOCTL command passed from the Block Layer.
  1760. * @arg IOCTL argument passed from the Block Layer.
  1761. *
  1762. * return value
  1763. * 0 The IOCTL completed successfully.
  1764. * -ENOTTY The specified command is not supported.
  1765. * -EFAULT An error occurred copying data to a user space buffer.
  1766. * -EIO An error occurred while executing the command.
  1767. */
  1768. static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
  1769. unsigned long arg)
  1770. {
  1771. switch (cmd) {
  1772. case HDIO_GET_IDENTITY:
  1773. if (mtip_get_identify(dd->port, (void __user *) arg) < 0) {
  1774. dev_warn(&dd->pdev->dev,
  1775. "Unable to read identity\n");
  1776. return -EIO;
  1777. }
  1778. break;
  1779. case HDIO_DRIVE_CMD:
  1780. {
  1781. u8 drive_command[4];
  1782. /* Copy the user command info to our buffer. */
  1783. if (copy_from_user(drive_command,
  1784. (void __user *) arg,
  1785. sizeof(drive_command)))
  1786. return -EFAULT;
  1787. /* Execute the drive command. */
  1788. if (exec_drive_command(dd->port,
  1789. drive_command,
  1790. (void __user *) (arg+4)))
  1791. return -EIO;
  1792. /* Copy the status back to the users buffer. */
  1793. if (copy_to_user((void __user *) arg,
  1794. drive_command,
  1795. sizeof(drive_command)))
  1796. return -EFAULT;
  1797. break;
  1798. }
  1799. case HDIO_DRIVE_TASK:
  1800. {
  1801. u8 drive_command[7];
  1802. /* Copy the user command info to our buffer. */
  1803. if (copy_from_user(drive_command,
  1804. (void __user *) arg,
  1805. sizeof(drive_command)))
  1806. return -EFAULT;
  1807. /* Execute the drive command. */
  1808. if (exec_drive_task(dd->port, drive_command))
  1809. return -EIO;
  1810. /* Copy the status back to the users buffer. */
  1811. if (copy_to_user((void __user *) arg,
  1812. drive_command,
  1813. sizeof(drive_command)))
  1814. return -EFAULT;
  1815. break;
  1816. }
  1817. case HDIO_DRIVE_TASKFILE: {
  1818. ide_task_request_t req_task;
  1819. int ret, outtotal;
  1820. if (copy_from_user(&req_task, (void __user *) arg,
  1821. sizeof(req_task)))
  1822. return -EFAULT;
  1823. outtotal = sizeof(req_task);
  1824. ret = exec_drive_taskfile(dd, (void __user *) arg,
  1825. &req_task, outtotal);
  1826. if (copy_to_user((void __user *) arg, &req_task,
  1827. sizeof(req_task)))
  1828. return -EFAULT;
  1829. return ret;
  1830. }
  1831. default:
  1832. return -EINVAL;
  1833. }
  1834. return 0;
  1835. }
  1836. /*
  1837. * Submit an IO to the hw
  1838. *
  1839. * This function is called by the block layer to issue an io
  1840. * to the device. Upon completion, the callback function will
  1841. * be called with the data parameter passed as the callback data.
  1842. *
  1843. * @dd Pointer to the driver data structure.
  1844. * @start First sector to read.
  1845. * @nsect Number of sectors to read.
  1846. * @nents Number of entries in scatter list for the read command.
  1847. * @tag The tag of this read command.
  1848. * @callback Pointer to the function that should be called
  1849. * when the read completes.
  1850. * @data Callback data passed to the callback function
  1851. * when the read completes.
  1852. * @dir Direction (read or write)
  1853. *
  1854. * return value
  1855. * None
  1856. */
  1857. static void mtip_hw_submit_io(struct driver_data *dd, sector_t start,
  1858. int nsect, int nents, int tag, void *callback,
  1859. void *data, int dir)
  1860. {
  1861. struct host_to_dev_fis *fis;
  1862. struct mtip_port *port = dd->port;
  1863. struct mtip_cmd *command = &port->commands[tag];
  1864. int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1865. /* Map the scatter list for DMA access */
  1866. nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
  1867. command->scatter_ents = nents;
  1868. /*
  1869. * The number of retries for this command before it is
  1870. * reported as a failure to the upper layers.
  1871. */
  1872. command->retries = MTIP_MAX_RETRIES;
  1873. /* Fill out fis */
  1874. fis = command->command;
  1875. fis->type = 0x27;
  1876. fis->opts = 1 << 7;
  1877. fis->command =
  1878. (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
  1879. *((unsigned int *) &fis->lba_low) = (start & 0xFFFFFF);
  1880. *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xFFFFFF);
  1881. fis->device = 1 << 6;
  1882. fis->features = nsect & 0xFF;
  1883. fis->features_ex = (nsect >> 8) & 0xFF;
  1884. fis->sect_count = ((tag << 3) | (tag >> 5));
  1885. fis->sect_cnt_ex = 0;
  1886. fis->control = 0;
  1887. fis->res2 = 0;
  1888. fis->res3 = 0;
  1889. fill_command_sg(dd, command, nents);
  1890. /* Populate the command header */
  1891. command->command_header->opts =
  1892. __force_bit2int cpu_to_le32(
  1893. (nents << 16) | 5 | AHCI_CMD_PREFETCH);
  1894. command->command_header->byte_count = 0;
  1895. /*
  1896. * Set the completion function and data for the command
  1897. * within this layer.
  1898. */
  1899. command->comp_data = dd;
  1900. command->comp_func = mtip_async_complete;
  1901. command->direction = dma_dir;
  1902. /*
  1903. * Set the completion function and data for the command passed
  1904. * from the upper layer.
  1905. */
  1906. command->async_data = data;
  1907. command->async_callback = callback;
  1908. /*
  1909. * To prevent this command from being issued
  1910. * if an internal command is in progress or error handling is active.
  1911. */
  1912. if (unlikely(test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) ||
  1913. test_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags))) {
  1914. set_bit(tag, port->cmds_to_issue);
  1915. set_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags);
  1916. return;
  1917. }
  1918. /* Issue the command to the hardware */
  1919. mtip_issue_ncq_command(port, tag);
  1920. return;
  1921. }
  1922. /*
  1923. * Release a command slot.
  1924. *
  1925. * @dd Pointer to the driver data structure.
  1926. * @tag Slot tag
  1927. *
  1928. * return value
  1929. * None
  1930. */
  1931. static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
  1932. {
  1933. release_slot(dd->port, tag);
  1934. }
  1935. /*
  1936. * Obtain a command slot and return its associated scatter list.
  1937. *
  1938. * @dd Pointer to the driver data structure.
  1939. * @tag Pointer to an int that will receive the allocated command
  1940. * slot tag.
  1941. *
  1942. * return value
  1943. * Pointer to the scatter list for the allocated command slot
  1944. * or NULL if no command slots are available.
  1945. */
  1946. static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
  1947. int *tag)
  1948. {
  1949. /*
  1950. * It is possible that, even with this semaphore, a thread
  1951. * may think that no command slots are available. Therefore, we
  1952. * need to make an attempt to get_slot().
  1953. */
  1954. down(&dd->port->cmd_slot);
  1955. *tag = get_slot(dd->port);
  1956. if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag))) {
  1957. up(&dd->port->cmd_slot);
  1958. return NULL;
  1959. }
  1960. if (unlikely(*tag < 0))
  1961. return NULL;
  1962. return dd->port->commands[*tag].sg;
  1963. }
  1964. /*
  1965. * Sysfs register/status dump.
  1966. *
  1967. * @dev Pointer to the device structure, passed by the kernrel.
  1968. * @attr Pointer to the device_attribute structure passed by the kernel.
  1969. * @buf Pointer to the char buffer that will receive the stats info.
  1970. *
  1971. * return value
  1972. * The size, in bytes, of the data copied into buf.
  1973. */
  1974. static ssize_t mtip_hw_show_registers(struct device *dev,
  1975. struct device_attribute *attr,
  1976. char *buf)
  1977. {
  1978. u32 group_allocated;
  1979. struct driver_data *dd = dev_to_disk(dev)->private_data;
  1980. int size = 0;
  1981. int n;
  1982. size += sprintf(&buf[size], "%s:\ns_active:\n", __func__);
  1983. for (n = 0; n < dd->slot_groups; n++)
  1984. size += sprintf(&buf[size], "0x%08x\n",
  1985. readl(dd->port->s_active[n]));
  1986. size += sprintf(&buf[size], "Command Issue:\n");
  1987. for (n = 0; n < dd->slot_groups; n++)
  1988. size += sprintf(&buf[size], "0x%08x\n",
  1989. readl(dd->port->cmd_issue[n]));
  1990. size += sprintf(&buf[size], "Allocated:\n");
  1991. for (n = 0; n < dd->slot_groups; n++) {
  1992. if (sizeof(long) > sizeof(u32))
  1993. group_allocated =
  1994. dd->port->allocated[n/2] >> (32*(n&1));
  1995. else
  1996. group_allocated = dd->port->allocated[n];
  1997. size += sprintf(&buf[size], "0x%08x\n",
  1998. group_allocated);
  1999. }
  2000. size += sprintf(&buf[size], "completed:\n");
  2001. for (n = 0; n < dd->slot_groups; n++)
  2002. size += sprintf(&buf[size], "0x%08x\n",
  2003. readl(dd->port->completed[n]));
  2004. size += sprintf(&buf[size], "PORT_IRQ_STAT 0x%08x\n",
  2005. readl(dd->port->mmio + PORT_IRQ_STAT));
  2006. size += sprintf(&buf[size], "HOST_IRQ_STAT 0x%08x\n",
  2007. readl(dd->mmio + HOST_IRQ_STAT));
  2008. return size;
  2009. }
  2010. static DEVICE_ATTR(registers, S_IRUGO, mtip_hw_show_registers, NULL);
  2011. /*
  2012. * Create the sysfs related attributes.
  2013. *
  2014. * @dd Pointer to the driver data structure.
  2015. * @kobj Pointer to the kobj for the block device.
  2016. *
  2017. * return value
  2018. * 0 Operation completed successfully.
  2019. * -EINVAL Invalid parameter.
  2020. */
  2021. static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
  2022. {
  2023. if (!kobj || !dd)
  2024. return -EINVAL;
  2025. if (sysfs_create_file(kobj, &dev_attr_registers.attr))
  2026. dev_warn(&dd->pdev->dev,
  2027. "Error creating registers sysfs entry\n");
  2028. return 0;
  2029. }
  2030. /*
  2031. * Remove the sysfs related attributes.
  2032. *
  2033. * @dd Pointer to the driver data structure.
  2034. * @kobj Pointer to the kobj for the block device.
  2035. *
  2036. * return value
  2037. * 0 Operation completed successfully.
  2038. * -EINVAL Invalid parameter.
  2039. */
  2040. static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
  2041. {
  2042. if (!kobj || !dd)
  2043. return -EINVAL;
  2044. sysfs_remove_file(kobj, &dev_attr_registers.attr);
  2045. return 0;
  2046. }
  2047. /*
  2048. * Perform any init/resume time hardware setup
  2049. *
  2050. * @dd Pointer to the driver data structure.
  2051. *
  2052. * return value
  2053. * None
  2054. */
  2055. static inline void hba_setup(struct driver_data *dd)
  2056. {
  2057. u32 hwdata;
  2058. hwdata = readl(dd->mmio + HOST_HSORG);
  2059. /* interrupt bug workaround: use only 1 IS bit.*/
  2060. writel(hwdata |
  2061. HSORG_DISABLE_SLOTGRP_INTR |
  2062. HSORG_DISABLE_SLOTGRP_PXIS,
  2063. dd->mmio + HOST_HSORG);
  2064. }
  2065. /*
  2066. * Detect the details of the product, and store anything needed
  2067. * into the driver data structure. This includes product type and
  2068. * version and number of slot groups.
  2069. *
  2070. * @dd Pointer to the driver data structure.
  2071. *
  2072. * return value
  2073. * None
  2074. */
  2075. static void mtip_detect_product(struct driver_data *dd)
  2076. {
  2077. u32 hwdata;
  2078. unsigned int rev, slotgroups;
  2079. /*
  2080. * HBA base + 0xFC [15:0] - vendor-specific hardware interface
  2081. * info register:
  2082. * [15:8] hardware/software interface rev#
  2083. * [ 3] asic-style interface
  2084. * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
  2085. */
  2086. hwdata = readl(dd->mmio + HOST_HSORG);
  2087. dd->product_type = MTIP_PRODUCT_UNKNOWN;
  2088. dd->slot_groups = 1;
  2089. if (hwdata & 0x8) {
  2090. dd->product_type = MTIP_PRODUCT_ASICFPGA;
  2091. rev = (hwdata & HSORG_HWREV) >> 8;
  2092. slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
  2093. dev_info(&dd->pdev->dev,
  2094. "ASIC-FPGA design, HS rev 0x%x, "
  2095. "%i slot groups [%i slots]\n",
  2096. rev,
  2097. slotgroups,
  2098. slotgroups * 32);
  2099. if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
  2100. dev_warn(&dd->pdev->dev,
  2101. "Warning: driver only supports "
  2102. "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
  2103. slotgroups = MTIP_MAX_SLOT_GROUPS;
  2104. }
  2105. dd->slot_groups = slotgroups;
  2106. return;
  2107. }
  2108. dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
  2109. }
  2110. /*
  2111. * Blocking wait for FTL rebuild to complete
  2112. *
  2113. * @dd Pointer to the DRIVER_DATA structure.
  2114. *
  2115. * return value
  2116. * 0 FTL rebuild completed successfully
  2117. * -EFAULT FTL rebuild error/timeout/interruption
  2118. */
  2119. static int mtip_ftl_rebuild_poll(struct driver_data *dd)
  2120. {
  2121. unsigned long timeout, cnt = 0, start;
  2122. dev_warn(&dd->pdev->dev,
  2123. "FTL rebuild in progress. Polling for completion.\n");
  2124. start = jiffies;
  2125. timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
  2126. do {
  2127. if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
  2128. &dd->dd_flag)))
  2129. return -EFAULT;
  2130. if (mtip_check_surprise_removal(dd->pdev))
  2131. return -EFAULT;
  2132. if (mtip_get_identify(dd->port, NULL) < 0)
  2133. return -EFAULT;
  2134. if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
  2135. MTIP_FTL_REBUILD_MAGIC) {
  2136. ssleep(1);
  2137. /* Print message every 3 minutes */
  2138. if (cnt++ >= 180) {
  2139. dev_warn(&dd->pdev->dev,
  2140. "FTL rebuild in progress (%d secs).\n",
  2141. jiffies_to_msecs(jiffies - start) / 1000);
  2142. cnt = 0;
  2143. }
  2144. } else {
  2145. dev_warn(&dd->pdev->dev,
  2146. "FTL rebuild complete (%d secs).\n",
  2147. jiffies_to_msecs(jiffies - start) / 1000);
  2148. mtip_block_initialize(dd);
  2149. return 0;
  2150. }
  2151. ssleep(10);
  2152. } while (time_before(jiffies, timeout));
  2153. /* Check for timeout */
  2154. dev_err(&dd->pdev->dev,
  2155. "Timed out waiting for FTL rebuild to complete (%d secs).\n",
  2156. jiffies_to_msecs(jiffies - start) / 1000);
  2157. return -EFAULT;
  2158. }
  2159. /*
  2160. * service thread to issue queued commands
  2161. *
  2162. * @data Pointer to the driver data structure.
  2163. *
  2164. * return value
  2165. * 0
  2166. */
  2167. static int mtip_service_thread(void *data)
  2168. {
  2169. struct driver_data *dd = (struct driver_data *)data;
  2170. unsigned long slot, slot_start, slot_wrap;
  2171. unsigned int num_cmd_slots = dd->slot_groups * 32;
  2172. struct mtip_port *port = dd->port;
  2173. while (1) {
  2174. /*
  2175. * the condition is to check neither an internal command is
  2176. * is in progress nor error handling is active
  2177. */
  2178. wait_event_interruptible(port->svc_wait, (port->flags) &&
  2179. !test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) &&
  2180. !test_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags));
  2181. if (kthread_should_stop())
  2182. break;
  2183. if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT,
  2184. &dd->dd_flag)))
  2185. break;
  2186. set_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags);
  2187. if (test_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags)) {
  2188. slot = 1;
  2189. /* used to restrict the loop to one iteration */
  2190. slot_start = num_cmd_slots;
  2191. slot_wrap = 0;
  2192. while (1) {
  2193. slot = find_next_bit(port->cmds_to_issue,
  2194. num_cmd_slots, slot);
  2195. if (slot_wrap == 1) {
  2196. if ((slot_start >= slot) ||
  2197. (slot >= num_cmd_slots))
  2198. break;
  2199. }
  2200. if (unlikely(slot_start == num_cmd_slots))
  2201. slot_start = slot;
  2202. if (unlikely(slot == num_cmd_slots)) {
  2203. slot = 1;
  2204. slot_wrap = 1;
  2205. continue;
  2206. }
  2207. /* Issue the command to the hardware */
  2208. mtip_issue_ncq_command(port, slot);
  2209. clear_bit(slot, port->cmds_to_issue);
  2210. }
  2211. clear_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags);
  2212. } else if (test_bit(MTIP_FLAG_REBUILD_BIT, &port->flags)) {
  2213. mtip_ftl_rebuild_poll(dd);
  2214. clear_bit(MTIP_FLAG_REBUILD_BIT, &port->flags);
  2215. }
  2216. clear_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags);
  2217. if (test_bit(MTIP_FLAG_SVC_THD_SHOULD_STOP_BIT, &port->flags))
  2218. break;
  2219. }
  2220. return 0;
  2221. }
  2222. /*
  2223. * Called once for each card.
  2224. *
  2225. * @dd Pointer to the driver data structure.
  2226. *
  2227. * return value
  2228. * 0 on success, else an error code.
  2229. */
  2230. static int mtip_hw_init(struct driver_data *dd)
  2231. {
  2232. int i;
  2233. int rv;
  2234. unsigned int num_command_slots;
  2235. unsigned long timeout, timetaken;
  2236. dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
  2237. mtip_detect_product(dd);
  2238. if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
  2239. rv = -EIO;
  2240. goto out1;
  2241. }
  2242. num_command_slots = dd->slot_groups * 32;
  2243. hba_setup(dd);
  2244. tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
  2245. dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
  2246. if (!dd->port) {
  2247. dev_err(&dd->pdev->dev,
  2248. "Memory allocation: port structure\n");
  2249. return -ENOMEM;
  2250. }
  2251. /* Counting semaphore to track command slot usage */
  2252. sema_init(&dd->port->cmd_slot, num_command_slots - 1);
  2253. /* Spinlock to prevent concurrent issue */
  2254. spin_lock_init(&dd->port->cmd_issue_lock);
  2255. /* Set the port mmio base address. */
  2256. dd->port->mmio = dd->mmio + PORT_OFFSET;
  2257. dd->port->dd = dd;
  2258. /* Allocate memory for the command list. */
  2259. dd->port->command_list =
  2260. dmam_alloc_coherent(&dd->pdev->dev,
  2261. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
  2262. &dd->port->command_list_dma,
  2263. GFP_KERNEL);
  2264. if (!dd->port->command_list) {
  2265. dev_err(&dd->pdev->dev,
  2266. "Memory allocation: command list\n");
  2267. rv = -ENOMEM;
  2268. goto out1;
  2269. }
  2270. /* Clear the memory we have allocated. */
  2271. memset(dd->port->command_list,
  2272. 0,
  2273. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2));
  2274. /* Setup the addresse of the RX FIS. */
  2275. dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
  2276. dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
  2277. /* Setup the address of the command tables. */
  2278. dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
  2279. dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
  2280. /* Setup the address of the identify data. */
  2281. dd->port->identify = dd->port->command_table +
  2282. HW_CMD_TBL_AR_SZ;
  2283. dd->port->identify_dma = dd->port->command_tbl_dma +
  2284. HW_CMD_TBL_AR_SZ;
  2285. /* Setup the address of the sector buffer. */
  2286. dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
  2287. dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
  2288. /* Point the command headers at the command tables. */
  2289. for (i = 0; i < num_command_slots; i++) {
  2290. dd->port->commands[i].command_header =
  2291. dd->port->command_list +
  2292. (sizeof(struct mtip_cmd_hdr) * i);
  2293. dd->port->commands[i].command_header_dma =
  2294. dd->port->command_list_dma +
  2295. (sizeof(struct mtip_cmd_hdr) * i);
  2296. dd->port->commands[i].command =
  2297. dd->port->command_table + (HW_CMD_TBL_SZ * i);
  2298. dd->port->commands[i].command_dma =
  2299. dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
  2300. if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
  2301. dd->port->commands[i].command_header->ctbau =
  2302. __force_bit2int cpu_to_le32(
  2303. (dd->port->commands[i].command_dma >> 16) >> 16);
  2304. dd->port->commands[i].command_header->ctba =
  2305. __force_bit2int cpu_to_le32(
  2306. dd->port->commands[i].command_dma & 0xFFFFFFFF);
  2307. /*
  2308. * If this is not done, a bug is reported by the stock
  2309. * FC11 i386. Due to the fact that it has lots of kernel
  2310. * debugging enabled.
  2311. */
  2312. sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
  2313. /* Mark all commands as currently inactive.*/
  2314. atomic_set(&dd->port->commands[i].active, 0);
  2315. }
  2316. /* Setup the pointers to the extended s_active and CI registers. */
  2317. for (i = 0; i < dd->slot_groups; i++) {
  2318. dd->port->s_active[i] =
  2319. dd->port->mmio + i*0x80 + PORT_SCR_ACT;
  2320. dd->port->cmd_issue[i] =
  2321. dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
  2322. dd->port->completed[i] =
  2323. dd->port->mmio + i*0x80 + PORT_SDBV;
  2324. }
  2325. timetaken = jiffies;
  2326. timeout = jiffies + msecs_to_jiffies(30000);
  2327. while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
  2328. time_before(jiffies, timeout)) {
  2329. mdelay(100);
  2330. }
  2331. if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
  2332. timetaken = jiffies - timetaken;
  2333. dev_warn(&dd->pdev->dev,
  2334. "Surprise removal detected at %u ms\n",
  2335. jiffies_to_msecs(timetaken));
  2336. rv = -ENODEV;
  2337. goto out2 ;
  2338. }
  2339. if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag))) {
  2340. timetaken = jiffies - timetaken;
  2341. dev_warn(&dd->pdev->dev,
  2342. "Removal detected at %u ms\n",
  2343. jiffies_to_msecs(timetaken));
  2344. rv = -EFAULT;
  2345. goto out2;
  2346. }
  2347. /* Conditionally reset the HBA. */
  2348. if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
  2349. if (mtip_hba_reset(dd) < 0) {
  2350. dev_err(&dd->pdev->dev,
  2351. "Card did not reset within timeout\n");
  2352. rv = -EIO;
  2353. goto out2;
  2354. }
  2355. } else {
  2356. /* Clear any pending interrupts on the HBA */
  2357. writel(readl(dd->mmio + HOST_IRQ_STAT),
  2358. dd->mmio + HOST_IRQ_STAT);
  2359. }
  2360. mtip_init_port(dd->port);
  2361. mtip_start_port(dd->port);
  2362. /* Setup the ISR and enable interrupts. */
  2363. rv = devm_request_irq(&dd->pdev->dev,
  2364. dd->pdev->irq,
  2365. mtip_irq_handler,
  2366. IRQF_SHARED,
  2367. dev_driver_string(&dd->pdev->dev),
  2368. dd);
  2369. if (rv) {
  2370. dev_err(&dd->pdev->dev,
  2371. "Unable to allocate IRQ %d\n", dd->pdev->irq);
  2372. goto out2;
  2373. }
  2374. /* Enable interrupts on the HBA. */
  2375. writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
  2376. dd->mmio + HOST_CTL);
  2377. init_timer(&dd->port->cmd_timer);
  2378. init_waitqueue_head(&dd->port->svc_wait);
  2379. dd->port->cmd_timer.data = (unsigned long int) dd->port;
  2380. dd->port->cmd_timer.function = mtip_timeout_function;
  2381. mod_timer(&dd->port->cmd_timer,
  2382. jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
  2383. if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag)) {
  2384. rv = -EFAULT;
  2385. goto out3;
  2386. }
  2387. if (mtip_get_identify(dd->port, NULL) < 0) {
  2388. rv = -EFAULT;
  2389. goto out3;
  2390. }
  2391. if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
  2392. MTIP_FTL_REBUILD_MAGIC) {
  2393. set_bit(MTIP_FLAG_REBUILD_BIT, &dd->port->flags);
  2394. return MTIP_FTL_REBUILD_MAGIC;
  2395. }
  2396. mtip_dump_identify(dd->port);
  2397. return rv;
  2398. out3:
  2399. del_timer_sync(&dd->port->cmd_timer);
  2400. /* Disable interrupts on the HBA. */
  2401. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2402. dd->mmio + HOST_CTL);
  2403. /*Release the IRQ. */
  2404. devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
  2405. out2:
  2406. mtip_deinit_port(dd->port);
  2407. /* Free the command/command header memory. */
  2408. dmam_free_coherent(&dd->pdev->dev,
  2409. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
  2410. dd->port->command_list,
  2411. dd->port->command_list_dma);
  2412. out1:
  2413. /* Free the memory allocated for the for structure. */
  2414. kfree(dd->port);
  2415. return rv;
  2416. }
  2417. /*
  2418. * Called to deinitialize an interface.
  2419. *
  2420. * @dd Pointer to the driver data structure.
  2421. *
  2422. * return value
  2423. * 0
  2424. */
  2425. static int mtip_hw_exit(struct driver_data *dd)
  2426. {
  2427. /*
  2428. * Send standby immediate (E0h) to the drive so that it
  2429. * saves its state.
  2430. */
  2431. if (!test_bit(MTIP_DD_FLAG_CLEANUP_BIT, &dd->dd_flag)) {
  2432. if (test_bit(MTIP_FLAG_REBUILD_BIT, &dd->dd_flag))
  2433. if (mtip_standby_immediate(dd->port))
  2434. dev_warn(&dd->pdev->dev,
  2435. "STANDBY IMMEDIATE failed\n");
  2436. /* de-initialize the port. */
  2437. mtip_deinit_port(dd->port);
  2438. /* Disable interrupts on the HBA. */
  2439. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2440. dd->mmio + HOST_CTL);
  2441. }
  2442. del_timer_sync(&dd->port->cmd_timer);
  2443. /* Release the IRQ. */
  2444. devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
  2445. /* Stop the bottom half tasklet. */
  2446. tasklet_kill(&dd->tasklet);
  2447. /* Free the command/command header memory. */
  2448. dmam_free_coherent(&dd->pdev->dev,
  2449. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
  2450. dd->port->command_list,
  2451. dd->port->command_list_dma);
  2452. /* Free the memory allocated for the for structure. */
  2453. kfree(dd->port);
  2454. return 0;
  2455. }
  2456. /*
  2457. * Issue a Standby Immediate command to the device.
  2458. *
  2459. * This function is called by the Block Layer just before the
  2460. * system powers off during a shutdown.
  2461. *
  2462. * @dd Pointer to the driver data structure.
  2463. *
  2464. * return value
  2465. * 0
  2466. */
  2467. static int mtip_hw_shutdown(struct driver_data *dd)
  2468. {
  2469. /*
  2470. * Send standby immediate (E0h) to the drive so that it
  2471. * saves its state.
  2472. */
  2473. mtip_standby_immediate(dd->port);
  2474. return 0;
  2475. }
  2476. /*
  2477. * Suspend function
  2478. *
  2479. * This function is called by the Block Layer just before the
  2480. * system hibernates.
  2481. *
  2482. * @dd Pointer to the driver data structure.
  2483. *
  2484. * return value
  2485. * 0 Suspend was successful
  2486. * -EFAULT Suspend was not successful
  2487. */
  2488. static int mtip_hw_suspend(struct driver_data *dd)
  2489. {
  2490. /*
  2491. * Send standby immediate (E0h) to the drive
  2492. * so that it saves its state.
  2493. */
  2494. if (mtip_standby_immediate(dd->port) != 0) {
  2495. dev_err(&dd->pdev->dev,
  2496. "Failed standby-immediate command\n");
  2497. return -EFAULT;
  2498. }
  2499. /* Disable interrupts on the HBA.*/
  2500. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2501. dd->mmio + HOST_CTL);
  2502. mtip_deinit_port(dd->port);
  2503. return 0;
  2504. }
  2505. /*
  2506. * Resume function
  2507. *
  2508. * This function is called by the Block Layer as the
  2509. * system resumes.
  2510. *
  2511. * @dd Pointer to the driver data structure.
  2512. *
  2513. * return value
  2514. * 0 Resume was successful
  2515. * -EFAULT Resume was not successful
  2516. */
  2517. static int mtip_hw_resume(struct driver_data *dd)
  2518. {
  2519. /* Perform any needed hardware setup steps */
  2520. hba_setup(dd);
  2521. /* Reset the HBA */
  2522. if (mtip_hba_reset(dd) != 0) {
  2523. dev_err(&dd->pdev->dev,
  2524. "Unable to reset the HBA\n");
  2525. return -EFAULT;
  2526. }
  2527. /*
  2528. * Enable the port, DMA engine, and FIS reception specific
  2529. * h/w in controller.
  2530. */
  2531. mtip_init_port(dd->port);
  2532. mtip_start_port(dd->port);
  2533. /* Enable interrupts on the HBA.*/
  2534. writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
  2535. dd->mmio + HOST_CTL);
  2536. return 0;
  2537. }
  2538. /*
  2539. * Helper function for reusing disk name
  2540. * upon hot insertion.
  2541. */
  2542. static int rssd_disk_name_format(char *prefix,
  2543. int index,
  2544. char *buf,
  2545. int buflen)
  2546. {
  2547. const int base = 'z' - 'a' + 1;
  2548. char *begin = buf + strlen(prefix);
  2549. char *end = buf + buflen;
  2550. char *p;
  2551. int unit;
  2552. p = end - 1;
  2553. *p = '\0';
  2554. unit = base;
  2555. do {
  2556. if (p == begin)
  2557. return -EINVAL;
  2558. *--p = 'a' + (index % unit);
  2559. index = (index / unit) - 1;
  2560. } while (index >= 0);
  2561. memmove(begin, p, end - p);
  2562. memcpy(buf, prefix, strlen(prefix));
  2563. return 0;
  2564. }
  2565. /*
  2566. * Block layer IOCTL handler.
  2567. *
  2568. * @dev Pointer to the block_device structure.
  2569. * @mode ignored
  2570. * @cmd IOCTL command passed from the user application.
  2571. * @arg Argument passed from the user application.
  2572. *
  2573. * return value
  2574. * 0 IOCTL completed successfully.
  2575. * -ENOTTY IOCTL not supported or invalid driver data
  2576. * structure pointer.
  2577. */
  2578. static int mtip_block_ioctl(struct block_device *dev,
  2579. fmode_t mode,
  2580. unsigned cmd,
  2581. unsigned long arg)
  2582. {
  2583. struct driver_data *dd = dev->bd_disk->private_data;
  2584. if (!capable(CAP_SYS_ADMIN))
  2585. return -EACCES;
  2586. if (!dd)
  2587. return -ENOTTY;
  2588. if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag)))
  2589. return -ENOTTY;
  2590. switch (cmd) {
  2591. case BLKFLSBUF:
  2592. return -ENOTTY;
  2593. default:
  2594. return mtip_hw_ioctl(dd, cmd, arg);
  2595. }
  2596. }
  2597. #ifdef CONFIG_COMPAT
  2598. /*
  2599. * Block layer compat IOCTL handler.
  2600. *
  2601. * @dev Pointer to the block_device structure.
  2602. * @mode ignored
  2603. * @cmd IOCTL command passed from the user application.
  2604. * @arg Argument passed from the user application.
  2605. *
  2606. * return value
  2607. * 0 IOCTL completed successfully.
  2608. * -ENOTTY IOCTL not supported or invalid driver data
  2609. * structure pointer.
  2610. */
  2611. static int mtip_block_compat_ioctl(struct block_device *dev,
  2612. fmode_t mode,
  2613. unsigned cmd,
  2614. unsigned long arg)
  2615. {
  2616. struct driver_data *dd = dev->bd_disk->private_data;
  2617. if (!capable(CAP_SYS_ADMIN))
  2618. return -EACCES;
  2619. if (!dd)
  2620. return -ENOTTY;
  2621. if (unlikely(test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag)))
  2622. return -ENOTTY;
  2623. switch (cmd) {
  2624. case BLKFLSBUF:
  2625. return -ENOTTY;
  2626. case HDIO_DRIVE_TASKFILE: {
  2627. struct mtip_compat_ide_task_request_s __user *compat_req_task;
  2628. ide_task_request_t req_task;
  2629. int compat_tasksize, outtotal, ret;
  2630. compat_tasksize =
  2631. sizeof(struct mtip_compat_ide_task_request_s);
  2632. compat_req_task =
  2633. (struct mtip_compat_ide_task_request_s __user *) arg;
  2634. if (copy_from_user(&req_task, (void __user *) arg,
  2635. compat_tasksize - (2 * sizeof(compat_long_t))))
  2636. return -EFAULT;
  2637. if (get_user(req_task.out_size, &compat_req_task->out_size))
  2638. return -EFAULT;
  2639. if (get_user(req_task.in_size, &compat_req_task->in_size))
  2640. return -EFAULT;
  2641. outtotal = sizeof(struct mtip_compat_ide_task_request_s);
  2642. ret = exec_drive_taskfile(dd, (void __user *) arg,
  2643. &req_task, outtotal);
  2644. if (copy_to_user((void __user *) arg, &req_task,
  2645. compat_tasksize -
  2646. (2 * sizeof(compat_long_t))))
  2647. return -EFAULT;
  2648. if (put_user(req_task.out_size, &compat_req_task->out_size))
  2649. return -EFAULT;
  2650. if (put_user(req_task.in_size, &compat_req_task->in_size))
  2651. return -EFAULT;
  2652. return ret;
  2653. }
  2654. default:
  2655. return mtip_hw_ioctl(dd, cmd, arg);
  2656. }
  2657. }
  2658. #endif
  2659. /*
  2660. * Obtain the geometry of the device.
  2661. *
  2662. * You may think that this function is obsolete, but some applications,
  2663. * fdisk for example still used CHS values. This function describes the
  2664. * device as having 224 heads and 56 sectors per cylinder. These values are
  2665. * chosen so that each cylinder is aligned on a 4KB boundary. Since a
  2666. * partition is described in terms of a start and end cylinder this means
  2667. * that each partition is also 4KB aligned. Non-aligned partitions adversely
  2668. * affects performance.
  2669. *
  2670. * @dev Pointer to the block_device strucutre.
  2671. * @geo Pointer to a hd_geometry structure.
  2672. *
  2673. * return value
  2674. * 0 Operation completed successfully.
  2675. * -ENOTTY An error occurred while reading the drive capacity.
  2676. */
  2677. static int mtip_block_getgeo(struct block_device *dev,
  2678. struct hd_geometry *geo)
  2679. {
  2680. struct driver_data *dd = dev->bd_disk->private_data;
  2681. sector_t capacity;
  2682. if (!dd)
  2683. return -ENOTTY;
  2684. if (!(mtip_hw_get_capacity(dd, &capacity))) {
  2685. dev_warn(&dd->pdev->dev,
  2686. "Could not get drive capacity.\n");
  2687. return -ENOTTY;
  2688. }
  2689. geo->heads = 224;
  2690. geo->sectors = 56;
  2691. sector_div(capacity, (geo->heads * geo->sectors));
  2692. geo->cylinders = capacity;
  2693. return 0;
  2694. }
  2695. /*
  2696. * Block device operation function.
  2697. *
  2698. * This structure contains pointers to the functions required by the block
  2699. * layer.
  2700. */
  2701. static const struct block_device_operations mtip_block_ops = {
  2702. .ioctl = mtip_block_ioctl,
  2703. #ifdef CONFIG_COMPAT
  2704. .compat_ioctl = mtip_block_compat_ioctl,
  2705. #endif
  2706. .getgeo = mtip_block_getgeo,
  2707. .owner = THIS_MODULE
  2708. };
  2709. /*
  2710. * Block layer make request function.
  2711. *
  2712. * This function is called by the kernel to process a BIO for
  2713. * the P320 device.
  2714. *
  2715. * @queue Pointer to the request queue. Unused other than to obtain
  2716. * the driver data structure.
  2717. * @bio Pointer to the BIO.
  2718. *
  2719. */
  2720. static void mtip_make_request(struct request_queue *queue, struct bio *bio)
  2721. {
  2722. struct driver_data *dd = queue->queuedata;
  2723. struct scatterlist *sg;
  2724. struct bio_vec *bvec;
  2725. int nents = 0;
  2726. int tag = 0;
  2727. if (test_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag)) {
  2728. bio_endio(bio, -ENXIO);
  2729. return;
  2730. }
  2731. if (unlikely(!bio_has_data(bio))) {
  2732. blk_queue_flush(queue, 0);
  2733. bio_endio(bio, 0);
  2734. return;
  2735. }
  2736. sg = mtip_hw_get_scatterlist(dd, &tag);
  2737. if (likely(sg != NULL)) {
  2738. blk_queue_bounce(queue, &bio);
  2739. if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
  2740. dev_warn(&dd->pdev->dev,
  2741. "Maximum number of SGL entries exceeded\n");
  2742. bio_io_error(bio);
  2743. mtip_hw_release_scatterlist(dd, tag);
  2744. return;
  2745. }
  2746. /* Create the scatter list for this bio. */
  2747. bio_for_each_segment(bvec, bio, nents) {
  2748. sg_set_page(&sg[nents],
  2749. bvec->bv_page,
  2750. bvec->bv_len,
  2751. bvec->bv_offset);
  2752. }
  2753. /* Issue the read/write. */
  2754. mtip_hw_submit_io(dd,
  2755. bio->bi_sector,
  2756. bio_sectors(bio),
  2757. nents,
  2758. tag,
  2759. bio_endio,
  2760. bio,
  2761. bio_data_dir(bio));
  2762. } else
  2763. bio_io_error(bio);
  2764. }
  2765. /*
  2766. * Block layer initialization function.
  2767. *
  2768. * This function is called once by the PCI layer for each P320
  2769. * device that is connected to the system.
  2770. *
  2771. * @dd Pointer to the driver data structure.
  2772. *
  2773. * return value
  2774. * 0 on success else an error code.
  2775. */
  2776. static int mtip_block_initialize(struct driver_data *dd)
  2777. {
  2778. int rv = 0, wait_for_rebuild = 0;
  2779. sector_t capacity;
  2780. unsigned int index = 0;
  2781. struct kobject *kobj;
  2782. unsigned char thd_name[16];
  2783. if (dd->disk)
  2784. goto skip_create_disk; /* hw init done, before rebuild */
  2785. /* Initialize the protocol layer. */
  2786. wait_for_rebuild = mtip_hw_init(dd);
  2787. if (wait_for_rebuild < 0) {
  2788. dev_err(&dd->pdev->dev,
  2789. "Protocol layer initialization failed\n");
  2790. rv = -EINVAL;
  2791. goto protocol_init_error;
  2792. }
  2793. dd->disk = alloc_disk(MTIP_MAX_MINORS);
  2794. if (dd->disk == NULL) {
  2795. dev_err(&dd->pdev->dev,
  2796. "Unable to allocate gendisk structure\n");
  2797. rv = -EINVAL;
  2798. goto alloc_disk_error;
  2799. }
  2800. /* Generate the disk name, implemented same as in sd.c */
  2801. do {
  2802. if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
  2803. goto ida_get_error;
  2804. spin_lock(&rssd_index_lock);
  2805. rv = ida_get_new(&rssd_index_ida, &index);
  2806. spin_unlock(&rssd_index_lock);
  2807. } while (rv == -EAGAIN);
  2808. if (rv)
  2809. goto ida_get_error;
  2810. rv = rssd_disk_name_format("rssd",
  2811. index,
  2812. dd->disk->disk_name,
  2813. DISK_NAME_LEN);
  2814. if (rv)
  2815. goto disk_index_error;
  2816. dd->disk->driverfs_dev = &dd->pdev->dev;
  2817. dd->disk->major = dd->major;
  2818. dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
  2819. dd->disk->fops = &mtip_block_ops;
  2820. dd->disk->private_data = dd;
  2821. dd->index = index;
  2822. /*
  2823. * if rebuild pending, start the service thread, and delay the block
  2824. * queue creation and add_disk()
  2825. */
  2826. if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
  2827. goto start_service_thread;
  2828. skip_create_disk:
  2829. /* Allocate the request queue. */
  2830. dd->queue = blk_alloc_queue(GFP_KERNEL);
  2831. if (dd->queue == NULL) {
  2832. dev_err(&dd->pdev->dev,
  2833. "Unable to allocate request queue\n");
  2834. rv = -ENOMEM;
  2835. goto block_queue_alloc_init_error;
  2836. }
  2837. /* Attach our request function to the request queue. */
  2838. blk_queue_make_request(dd->queue, mtip_make_request);
  2839. dd->disk->queue = dd->queue;
  2840. dd->queue->queuedata = dd;
  2841. /* Set device limits. */
  2842. set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
  2843. blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
  2844. blk_queue_physical_block_size(dd->queue, 4096);
  2845. blk_queue_io_min(dd->queue, 4096);
  2846. /*
  2847. * write back cache is not supported in the device. FUA depends on
  2848. * write back cache support, hence setting flush support to zero.
  2849. */
  2850. blk_queue_flush(dd->queue, 0);
  2851. /* Set the capacity of the device in 512 byte sectors. */
  2852. if (!(mtip_hw_get_capacity(dd, &capacity))) {
  2853. dev_warn(&dd->pdev->dev,
  2854. "Could not read drive capacity\n");
  2855. rv = -EIO;
  2856. goto read_capacity_error;
  2857. }
  2858. set_capacity(dd->disk, capacity);
  2859. /* Enable the block device and add it to /dev */
  2860. add_disk(dd->disk);
  2861. /*
  2862. * Now that the disk is active, initialize any sysfs attributes
  2863. * managed by the protocol layer.
  2864. */
  2865. kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
  2866. if (kobj) {
  2867. mtip_hw_sysfs_init(dd, kobj);
  2868. kobject_put(kobj);
  2869. }
  2870. if (dd->mtip_svc_handler) {
  2871. set_bit(MTIP_DD_FLAG_INIT_DONE_BIT, &dd->dd_flag);
  2872. return rv; /* service thread created for handling rebuild */
  2873. }
  2874. start_service_thread:
  2875. sprintf(thd_name, "mtip_svc_thd_%02d", index);
  2876. dd->mtip_svc_handler = kthread_run(mtip_service_thread,
  2877. dd, thd_name);
  2878. if (IS_ERR(dd->mtip_svc_handler)) {
  2879. printk(KERN_ERR "mtip32xx: service thread failed to start\n");
  2880. dd->mtip_svc_handler = NULL;
  2881. rv = -EFAULT;
  2882. goto kthread_run_error;
  2883. }
  2884. if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
  2885. rv = wait_for_rebuild;
  2886. return rv;
  2887. kthread_run_error:
  2888. /* Delete our gendisk. This also removes the device from /dev */
  2889. del_gendisk(dd->disk);
  2890. read_capacity_error:
  2891. blk_cleanup_queue(dd->queue);
  2892. block_queue_alloc_init_error:
  2893. disk_index_error:
  2894. spin_lock(&rssd_index_lock);
  2895. ida_remove(&rssd_index_ida, index);
  2896. spin_unlock(&rssd_index_lock);
  2897. ida_get_error:
  2898. put_disk(dd->disk);
  2899. alloc_disk_error:
  2900. mtip_hw_exit(dd); /* De-initialize the protocol layer. */
  2901. protocol_init_error:
  2902. return rv;
  2903. }
  2904. /*
  2905. * Block layer deinitialization function.
  2906. *
  2907. * Called by the PCI layer as each P320 device is removed.
  2908. *
  2909. * @dd Pointer to the driver data structure.
  2910. *
  2911. * return value
  2912. * 0
  2913. */
  2914. static int mtip_block_remove(struct driver_data *dd)
  2915. {
  2916. struct kobject *kobj;
  2917. if (dd->mtip_svc_handler) {
  2918. set_bit(MTIP_FLAG_SVC_THD_SHOULD_STOP_BIT, &dd->port->flags);
  2919. wake_up_interruptible(&dd->port->svc_wait);
  2920. kthread_stop(dd->mtip_svc_handler);
  2921. }
  2922. /* Clean up the sysfs attributes managed by the protocol layer. */
  2923. if (test_bit(MTIP_DD_FLAG_INIT_DONE_BIT, &dd->dd_flag)) {
  2924. kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
  2925. if (kobj) {
  2926. mtip_hw_sysfs_exit(dd, kobj);
  2927. kobject_put(kobj);
  2928. }
  2929. }
  2930. /*
  2931. * Delete our gendisk structure. This also removes the device
  2932. * from /dev
  2933. */
  2934. del_gendisk(dd->disk);
  2935. blk_cleanup_queue(dd->queue);
  2936. dd->disk = NULL;
  2937. dd->queue = NULL;
  2938. /* De-initialize the protocol layer. */
  2939. mtip_hw_exit(dd);
  2940. return 0;
  2941. }
  2942. /*
  2943. * Function called by the PCI layer when just before the
  2944. * machine shuts down.
  2945. *
  2946. * If a protocol layer shutdown function is present it will be called
  2947. * by this function.
  2948. *
  2949. * @dd Pointer to the driver data structure.
  2950. *
  2951. * return value
  2952. * 0
  2953. */
  2954. static int mtip_block_shutdown(struct driver_data *dd)
  2955. {
  2956. dev_info(&dd->pdev->dev,
  2957. "Shutting down %s ...\n", dd->disk->disk_name);
  2958. /* Delete our gendisk structure, and cleanup the blk queue. */
  2959. del_gendisk(dd->disk);
  2960. blk_cleanup_queue(dd->queue);
  2961. dd->disk = NULL;
  2962. dd->queue = NULL;
  2963. mtip_hw_shutdown(dd);
  2964. return 0;
  2965. }
  2966. static int mtip_block_suspend(struct driver_data *dd)
  2967. {
  2968. dev_info(&dd->pdev->dev,
  2969. "Suspending %s ...\n", dd->disk->disk_name);
  2970. mtip_hw_suspend(dd);
  2971. return 0;
  2972. }
  2973. static int mtip_block_resume(struct driver_data *dd)
  2974. {
  2975. dev_info(&dd->pdev->dev, "Resuming %s ...\n",
  2976. dd->disk->disk_name);
  2977. mtip_hw_resume(dd);
  2978. return 0;
  2979. }
  2980. /*
  2981. * Called for each supported PCI device detected.
  2982. *
  2983. * This function allocates the private data structure, enables the
  2984. * PCI device and then calls the block layer initialization function.
  2985. *
  2986. * return value
  2987. * 0 on success else an error code.
  2988. */
  2989. static int mtip_pci_probe(struct pci_dev *pdev,
  2990. const struct pci_device_id *ent)
  2991. {
  2992. int rv = 0;
  2993. struct driver_data *dd = NULL;
  2994. /* Allocate memory for this devices private data. */
  2995. dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
  2996. if (dd == NULL) {
  2997. dev_err(&pdev->dev,
  2998. "Unable to allocate memory for driver data\n");
  2999. return -ENOMEM;
  3000. }
  3001. /* Attach the private data to this PCI device. */
  3002. pci_set_drvdata(pdev, dd);
  3003. rv = pcim_enable_device(pdev);
  3004. if (rv < 0) {
  3005. dev_err(&pdev->dev, "Unable to enable device\n");
  3006. goto iomap_err;
  3007. }
  3008. /* Map BAR5 to memory. */
  3009. rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
  3010. if (rv < 0) {
  3011. dev_err(&pdev->dev, "Unable to map regions\n");
  3012. goto iomap_err;
  3013. }
  3014. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  3015. rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  3016. if (rv) {
  3017. rv = pci_set_consistent_dma_mask(pdev,
  3018. DMA_BIT_MASK(32));
  3019. if (rv) {
  3020. dev_warn(&pdev->dev,
  3021. "64-bit DMA enable failed\n");
  3022. goto setmask_err;
  3023. }
  3024. }
  3025. }
  3026. pci_set_master(pdev);
  3027. if (pci_enable_msi(pdev)) {
  3028. dev_warn(&pdev->dev,
  3029. "Unable to enable MSI interrupt.\n");
  3030. goto block_initialize_err;
  3031. }
  3032. /* Copy the info we may need later into the private data structure. */
  3033. dd->major = mtip_major;
  3034. dd->instance = instance;
  3035. dd->pdev = pdev;
  3036. /* Initialize the block layer. */
  3037. rv = mtip_block_initialize(dd);
  3038. if (rv < 0) {
  3039. dev_err(&pdev->dev,
  3040. "Unable to initialize block layer\n");
  3041. goto block_initialize_err;
  3042. }
  3043. /*
  3044. * Increment the instance count so that each device has a unique
  3045. * instance number.
  3046. */
  3047. instance++;
  3048. if (rv != MTIP_FTL_REBUILD_MAGIC)
  3049. set_bit(MTIP_DD_FLAG_INIT_DONE_BIT, &dd->dd_flag);
  3050. goto done;
  3051. block_initialize_err:
  3052. pci_disable_msi(pdev);
  3053. setmask_err:
  3054. pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
  3055. iomap_err:
  3056. kfree(dd);
  3057. pci_set_drvdata(pdev, NULL);
  3058. return rv;
  3059. done:
  3060. return rv;
  3061. }
  3062. /*
  3063. * Called for each probed device when the device is removed or the
  3064. * driver is unloaded.
  3065. *
  3066. * return value
  3067. * None
  3068. */
  3069. static void mtip_pci_remove(struct pci_dev *pdev)
  3070. {
  3071. struct driver_data *dd = pci_get_drvdata(pdev);
  3072. int counter = 0;
  3073. set_bit(MTIP_DD_FLAG_REMOVE_PENDING_BIT, &dd->dd_flag);
  3074. if (mtip_check_surprise_removal(pdev)) {
  3075. while (!test_bit(MTIP_DD_FLAG_CLEANUP_BIT, &dd->dd_flag)) {
  3076. counter++;
  3077. msleep(20);
  3078. if (counter == 10) {
  3079. /* Cleanup the outstanding commands */
  3080. mtip_command_cleanup(dd);
  3081. break;
  3082. }
  3083. }
  3084. }
  3085. /* Clean up the block layer. */
  3086. mtip_block_remove(dd);
  3087. pci_disable_msi(pdev);
  3088. kfree(dd);
  3089. pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
  3090. }
  3091. /*
  3092. * Called for each probed device when the device is suspended.
  3093. *
  3094. * return value
  3095. * 0 Success
  3096. * <0 Error
  3097. */
  3098. static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
  3099. {
  3100. int rv = 0;
  3101. struct driver_data *dd = pci_get_drvdata(pdev);
  3102. if (!dd) {
  3103. dev_err(&pdev->dev,
  3104. "Driver private datastructure is NULL\n");
  3105. return -EFAULT;
  3106. }
  3107. set_bit(MTIP_DD_FLAG_RESUME_BIT, &dd->dd_flag);
  3108. /* Disable ports & interrupts then send standby immediate */
  3109. rv = mtip_block_suspend(dd);
  3110. if (rv < 0) {
  3111. dev_err(&pdev->dev,
  3112. "Failed to suspend controller\n");
  3113. return rv;
  3114. }
  3115. /*
  3116. * Save the pci config space to pdev structure &
  3117. * disable the device
  3118. */
  3119. pci_save_state(pdev);
  3120. pci_disable_device(pdev);
  3121. /* Move to Low power state*/
  3122. pci_set_power_state(pdev, PCI_D3hot);
  3123. return rv;
  3124. }
  3125. /*
  3126. * Called for each probed device when the device is resumed.
  3127. *
  3128. * return value
  3129. * 0 Success
  3130. * <0 Error
  3131. */
  3132. static int mtip_pci_resume(struct pci_dev *pdev)
  3133. {
  3134. int rv = 0;
  3135. struct driver_data *dd;
  3136. dd = pci_get_drvdata(pdev);
  3137. if (!dd) {
  3138. dev_err(&pdev->dev,
  3139. "Driver private datastructure is NULL\n");
  3140. return -EFAULT;
  3141. }
  3142. /* Move the device to active State */
  3143. pci_set_power_state(pdev, PCI_D0);
  3144. /* Restore PCI configuration space */
  3145. pci_restore_state(pdev);
  3146. /* Enable the PCI device*/
  3147. rv = pcim_enable_device(pdev);
  3148. if (rv < 0) {
  3149. dev_err(&pdev->dev,
  3150. "Failed to enable card during resume\n");
  3151. goto err;
  3152. }
  3153. pci_set_master(pdev);
  3154. /*
  3155. * Calls hbaReset, initPort, & startPort function
  3156. * then enables interrupts
  3157. */
  3158. rv = mtip_block_resume(dd);
  3159. if (rv < 0)
  3160. dev_err(&pdev->dev, "Unable to resume\n");
  3161. err:
  3162. clear_bit(MTIP_DD_FLAG_RESUME_BIT, &dd->dd_flag);
  3163. return rv;
  3164. }
  3165. /*
  3166. * Shutdown routine
  3167. *
  3168. * return value
  3169. * None
  3170. */
  3171. static void mtip_pci_shutdown(struct pci_dev *pdev)
  3172. {
  3173. struct driver_data *dd = pci_get_drvdata(pdev);
  3174. if (dd)
  3175. mtip_block_shutdown(dd);
  3176. }
  3177. /* Table of device ids supported by this driver. */
  3178. static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
  3179. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320_DEVICE_ID) },
  3180. { 0 }
  3181. };
  3182. /* Structure that describes the PCI driver functions. */
  3183. static struct pci_driver mtip_pci_driver = {
  3184. .name = MTIP_DRV_NAME,
  3185. .id_table = mtip_pci_tbl,
  3186. .probe = mtip_pci_probe,
  3187. .remove = mtip_pci_remove,
  3188. .suspend = mtip_pci_suspend,
  3189. .resume = mtip_pci_resume,
  3190. .shutdown = mtip_pci_shutdown,
  3191. };
  3192. MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
  3193. /*
  3194. * Module initialization function.
  3195. *
  3196. * Called once when the module is loaded. This function allocates a major
  3197. * block device number to the Cyclone devices and registers the PCI layer
  3198. * of the driver.
  3199. *
  3200. * Return value
  3201. * 0 on success else error code.
  3202. */
  3203. static int __init mtip_init(void)
  3204. {
  3205. int error;
  3206. printk(KERN_INFO MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
  3207. /* Allocate a major block device number to use with this driver. */
  3208. error = register_blkdev(0, MTIP_DRV_NAME);
  3209. if (error <= 0) {
  3210. printk(KERN_ERR "Unable to register block device (%d)\n",
  3211. error);
  3212. return -EBUSY;
  3213. }
  3214. mtip_major = error;
  3215. /* Register our PCI operations. */
  3216. error = pci_register_driver(&mtip_pci_driver);
  3217. if (error)
  3218. unregister_blkdev(mtip_major, MTIP_DRV_NAME);
  3219. return error;
  3220. }
  3221. /*
  3222. * Module de-initialization function.
  3223. *
  3224. * Called once when the module is unloaded. This function deallocates
  3225. * the major block device number allocated by mtip_init() and
  3226. * unregisters the PCI layer of the driver.
  3227. *
  3228. * Return value
  3229. * none
  3230. */
  3231. static void __exit mtip_exit(void)
  3232. {
  3233. /* Release the allocated major block device number. */
  3234. unregister_blkdev(mtip_major, MTIP_DRV_NAME);
  3235. /* Unregister the PCI driver. */
  3236. pci_unregister_driver(&mtip_pci_driver);
  3237. }
  3238. MODULE_AUTHOR("Micron Technology, Inc");
  3239. MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
  3240. MODULE_LICENSE("GPL");
  3241. MODULE_VERSION(MTIP_DRV_VERSION);
  3242. module_init(mtip_init);
  3243. module_exit(mtip_exit);