mtip32xx.c 115 KB

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