mtip32xx.c 87 KB

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