mtip32xx.c 111 KB

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