megaraid.c 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116
  1. /*
  2. *
  3. * Linux MegaRAID device driver
  4. *
  5. * Copyright (c) 2002 LSI Logic Corporation.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
  13. * - fixes
  14. * - speed-ups (list handling fixes, issued_list, optimizations.)
  15. * - lots of cleanups.
  16. *
  17. * Copyright (c) 2003 Christoph Hellwig <hch@lst.de>
  18. * - new-style, hotplug-aware pci probing and scsi registration
  19. *
  20. * Version : v2.00.4 Mon Nov 14 14:02:43 EST 2005 - Seokmann Ju
  21. * <Seokmann.Ju@lsil.com>
  22. *
  23. * Description: Linux device driver for LSI Logic MegaRAID controller
  24. *
  25. * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 471, 490, 493
  26. * 518, 520, 531, 532
  27. *
  28. * This driver is supported by LSI Logic, with assistance from Red Hat, Dell,
  29. * and others. Please send updates to the mailing list
  30. * linux-scsi@vger.kernel.org .
  31. *
  32. */
  33. #include <linux/mm.h>
  34. #include <linux/fs.h>
  35. #include <linux/blkdev.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/io.h>
  38. #include <linux/completion.h>
  39. #include <linux/delay.h>
  40. #include <linux/proc_fs.h>
  41. #include <linux/reboot.h>
  42. #include <linux/module.h>
  43. #include <linux/list.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/pci.h>
  46. #include <linux/init.h>
  47. #include <linux/dma-mapping.h>
  48. #include <scsi/scsicam.h>
  49. #include "scsi.h"
  50. #include <scsi/scsi_host.h>
  51. #include "megaraid.h"
  52. #define MEGARAID_MODULE_VERSION "2.00.4"
  53. MODULE_AUTHOR ("sju@lsil.com");
  54. MODULE_DESCRIPTION ("LSI Logic MegaRAID legacy driver");
  55. MODULE_LICENSE ("GPL");
  56. MODULE_VERSION(MEGARAID_MODULE_VERSION);
  57. static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
  58. module_param(max_cmd_per_lun, uint, 0);
  59. MODULE_PARM_DESC(max_cmd_per_lun, "Maximum number of commands which can be issued to a single LUN (default=DEF_CMD_PER_LUN=63)");
  60. static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
  61. module_param(max_sectors_per_io, ushort, 0);
  62. MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
  63. static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
  64. module_param(max_mbox_busy_wait, ushort, 0);
  65. MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
  66. #define RDINDOOR(adapter) readl((adapter)->base + 0x20)
  67. #define RDOUTDOOR(adapter) readl((adapter)->base + 0x2C)
  68. #define WRINDOOR(adapter,value) writel(value, (adapter)->base + 0x20)
  69. #define WROUTDOOR(adapter,value) writel(value, (adapter)->base + 0x2C)
  70. /*
  71. * Global variables
  72. */
  73. static int hba_count;
  74. static adapter_t *hba_soft_state[MAX_CONTROLLERS];
  75. static struct proc_dir_entry *mega_proc_dir_entry;
  76. /* For controller re-ordering */
  77. static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
  78. /*
  79. * The File Operations structure for the serial/ioctl interface of the driver
  80. */
  81. static struct file_operations megadev_fops = {
  82. .owner = THIS_MODULE,
  83. .ioctl = megadev_ioctl,
  84. .open = megadev_open,
  85. };
  86. /*
  87. * Array to structures for storing the information about the controllers. This
  88. * information is sent to the user level applications, when they do an ioctl
  89. * for this information.
  90. */
  91. static struct mcontroller mcontroller[MAX_CONTROLLERS];
  92. /* The current driver version */
  93. static u32 driver_ver = 0x02000000;
  94. /* major number used by the device for character interface */
  95. static int major;
  96. #define IS_RAID_CH(hba, ch) (((hba)->mega_ch_class >> (ch)) & 0x01)
  97. /*
  98. * Debug variable to print some diagnostic messages
  99. */
  100. static int trace_level;
  101. /**
  102. * mega_setup_mailbox()
  103. * @adapter - pointer to our soft state
  104. *
  105. * Allocates a 8 byte aligned memory for the handshake mailbox.
  106. */
  107. static int
  108. mega_setup_mailbox(adapter_t *adapter)
  109. {
  110. unsigned long align;
  111. adapter->una_mbox64 = pci_alloc_consistent(adapter->dev,
  112. sizeof(mbox64_t), &adapter->una_mbox64_dma);
  113. if( !adapter->una_mbox64 ) return -1;
  114. adapter->mbox = &adapter->una_mbox64->mbox;
  115. adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
  116. (~0UL ^ 0xFUL));
  117. adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
  118. align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
  119. adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
  120. /*
  121. * Register the mailbox if the controller is an io-mapped controller
  122. */
  123. if( adapter->flag & BOARD_IOMAP ) {
  124. outb_p(adapter->mbox_dma & 0xFF,
  125. adapter->host->io_port + MBOX_PORT0);
  126. outb_p((adapter->mbox_dma >> 8) & 0xFF,
  127. adapter->host->io_port + MBOX_PORT1);
  128. outb_p((adapter->mbox_dma >> 16) & 0xFF,
  129. adapter->host->io_port + MBOX_PORT2);
  130. outb_p((adapter->mbox_dma >> 24) & 0xFF,
  131. adapter->host->io_port + MBOX_PORT3);
  132. outb_p(ENABLE_MBOX_BYTE,
  133. adapter->host->io_port + ENABLE_MBOX_REGION);
  134. irq_ack(adapter);
  135. irq_enable(adapter);
  136. }
  137. return 0;
  138. }
  139. /*
  140. * mega_query_adapter()
  141. * @adapter - pointer to our soft state
  142. *
  143. * Issue the adapter inquiry commands to the controller and find out
  144. * information and parameter about the devices attached
  145. */
  146. static int
  147. mega_query_adapter(adapter_t *adapter)
  148. {
  149. dma_addr_t prod_info_dma_handle;
  150. mega_inquiry3 *inquiry3;
  151. u8 raw_mbox[sizeof(struct mbox_out)];
  152. mbox_t *mbox;
  153. int retval;
  154. /* Initialize adapter inquiry mailbox */
  155. mbox = (mbox_t *)raw_mbox;
  156. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  157. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  158. /*
  159. * Try to issue Inquiry3 command
  160. * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
  161. * update enquiry3 structure
  162. */
  163. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  164. inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
  165. raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
  166. raw_mbox[2] = NC_SUBOP_ENQUIRY3; /* i.e. 0x0F */
  167. raw_mbox[3] = ENQ3_GET_SOLICITED_FULL; /* i.e. 0x02 */
  168. /* Issue a blocking command to the card */
  169. if ((retval = issue_scb_block(adapter, raw_mbox))) {
  170. /* the adapter does not support 40ld */
  171. mraid_ext_inquiry *ext_inq;
  172. mraid_inquiry *inq;
  173. dma_addr_t dma_handle;
  174. ext_inq = pci_alloc_consistent(adapter->dev,
  175. sizeof(mraid_ext_inquiry), &dma_handle);
  176. if( ext_inq == NULL ) return -1;
  177. inq = &ext_inq->raid_inq;
  178. mbox->m_out.xferaddr = (u32)dma_handle;
  179. /*issue old 0x04 command to adapter */
  180. mbox->m_out.cmd = MEGA_MBOXCMD_ADPEXTINQ;
  181. issue_scb_block(adapter, raw_mbox);
  182. /*
  183. * update Enquiry3 and ProductInfo structures with
  184. * mraid_inquiry structure
  185. */
  186. mega_8_to_40ld(inq, inquiry3,
  187. (mega_product_info *)&adapter->product_info);
  188. pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
  189. ext_inq, dma_handle);
  190. } else { /*adapter supports 40ld */
  191. adapter->flag |= BOARD_40LD;
  192. /*
  193. * get product_info, which is static information and will be
  194. * unchanged
  195. */
  196. prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
  197. &adapter->product_info,
  198. sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
  199. mbox->m_out.xferaddr = prod_info_dma_handle;
  200. raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
  201. raw_mbox[2] = NC_SUBOP_PRODUCT_INFO; /* i.e. 0x0E */
  202. if ((retval = issue_scb_block(adapter, raw_mbox)))
  203. printk(KERN_WARNING
  204. "megaraid: Product_info cmd failed with error: %d\n",
  205. retval);
  206. pci_unmap_single(adapter->dev, prod_info_dma_handle,
  207. sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
  208. }
  209. /*
  210. * kernel scans the channels from 0 to <= max_channel
  211. */
  212. adapter->host->max_channel =
  213. adapter->product_info.nchannels + NVIRT_CHAN -1;
  214. adapter->host->max_id = 16; /* max targets per channel */
  215. adapter->host->max_lun = 7; /* Upto 7 luns for non disk devices */
  216. adapter->host->cmd_per_lun = max_cmd_per_lun;
  217. adapter->numldrv = inquiry3->num_ldrv;
  218. adapter->max_cmds = adapter->product_info.max_commands;
  219. if(adapter->max_cmds > MAX_COMMANDS)
  220. adapter->max_cmds = MAX_COMMANDS;
  221. adapter->host->can_queue = adapter->max_cmds - 1;
  222. /*
  223. * Get the maximum number of scatter-gather elements supported by this
  224. * firmware
  225. */
  226. mega_get_max_sgl(adapter);
  227. adapter->host->sg_tablesize = adapter->sglen;
  228. /* use HP firmware and bios version encoding */
  229. if (adapter->product_info.subsysvid == HP_SUBSYS_VID) {
  230. sprintf (adapter->fw_version, "%c%d%d.%d%d",
  231. adapter->product_info.fw_version[2],
  232. adapter->product_info.fw_version[1] >> 8,
  233. adapter->product_info.fw_version[1] & 0x0f,
  234. adapter->product_info.fw_version[0] >> 8,
  235. adapter->product_info.fw_version[0] & 0x0f);
  236. sprintf (adapter->bios_version, "%c%d%d.%d%d",
  237. adapter->product_info.bios_version[2],
  238. adapter->product_info.bios_version[1] >> 8,
  239. adapter->product_info.bios_version[1] & 0x0f,
  240. adapter->product_info.bios_version[0] >> 8,
  241. adapter->product_info.bios_version[0] & 0x0f);
  242. } else {
  243. memcpy(adapter->fw_version,
  244. (char *)adapter->product_info.fw_version, 4);
  245. adapter->fw_version[4] = 0;
  246. memcpy(adapter->bios_version,
  247. (char *)adapter->product_info.bios_version, 4);
  248. adapter->bios_version[4] = 0;
  249. }
  250. printk(KERN_NOTICE "megaraid: [%s:%s] detected %d logical drives.\n",
  251. adapter->fw_version, adapter->bios_version, adapter->numldrv);
  252. /*
  253. * Do we support extended (>10 bytes) cdbs
  254. */
  255. adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
  256. if (adapter->support_ext_cdb)
  257. printk(KERN_NOTICE "megaraid: supports extended CDBs.\n");
  258. return 0;
  259. }
  260. /**
  261. * mega_runpendq()
  262. * @adapter - pointer to our soft state
  263. *
  264. * Runs through the list of pending requests.
  265. */
  266. static inline void
  267. mega_runpendq(adapter_t *adapter)
  268. {
  269. if(!list_empty(&adapter->pending_list))
  270. __mega_runpendq(adapter);
  271. }
  272. /*
  273. * megaraid_queue()
  274. * @scmd - Issue this scsi command
  275. * @done - the callback hook into the scsi mid-layer
  276. *
  277. * The command queuing entry point for the mid-layer.
  278. */
  279. static int
  280. megaraid_queue(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
  281. {
  282. adapter_t *adapter;
  283. scb_t *scb;
  284. int busy=0;
  285. unsigned long flags;
  286. adapter = (adapter_t *)scmd->device->host->hostdata;
  287. scmd->scsi_done = done;
  288. /*
  289. * Allocate and build a SCB request
  290. * busy flag will be set if mega_build_cmd() command could not
  291. * allocate scb. We will return non-zero status in that case.
  292. * NOTE: scb can be null even though certain commands completed
  293. * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
  294. * return 0 in that case.
  295. */
  296. spin_lock_irqsave(&adapter->lock, flags);
  297. scb = mega_build_cmd(adapter, scmd, &busy);
  298. if (!scb)
  299. goto out;
  300. scb->state |= SCB_PENDQ;
  301. list_add_tail(&scb->list, &adapter->pending_list);
  302. /*
  303. * Check if the HBA is in quiescent state, e.g., during a
  304. * delete logical drive opertion. If it is, don't run
  305. * the pending_list.
  306. */
  307. if (atomic_read(&adapter->quiescent) == 0)
  308. mega_runpendq(adapter);
  309. busy = 0;
  310. out:
  311. spin_unlock_irqrestore(&adapter->lock, flags);
  312. return busy;
  313. }
  314. /**
  315. * mega_allocate_scb()
  316. * @adapter - pointer to our soft state
  317. * @cmd - scsi command from the mid-layer
  318. *
  319. * Allocate a SCB structure. This is the central structure for controller
  320. * commands.
  321. */
  322. static inline scb_t *
  323. mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
  324. {
  325. struct list_head *head = &adapter->free_list;
  326. scb_t *scb;
  327. /* Unlink command from Free List */
  328. if( !list_empty(head) ) {
  329. scb = list_entry(head->next, scb_t, list);
  330. list_del_init(head->next);
  331. scb->state = SCB_ACTIVE;
  332. scb->cmd = cmd;
  333. scb->dma_type = MEGA_DMA_TYPE_NONE;
  334. return scb;
  335. }
  336. return NULL;
  337. }
  338. /**
  339. * mega_get_ldrv_num()
  340. * @adapter - pointer to our soft state
  341. * @cmd - scsi mid layer command
  342. * @channel - channel on the controller
  343. *
  344. * Calculate the logical drive number based on the information in scsi command
  345. * and the channel number.
  346. */
  347. static inline int
  348. mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
  349. {
  350. int tgt;
  351. int ldrv_num;
  352. tgt = cmd->device->id;
  353. if ( tgt > adapter->this_id )
  354. tgt--; /* we do not get inquires for initiator id */
  355. ldrv_num = (channel * 15) + tgt;
  356. /*
  357. * If we have a logical drive with boot enabled, project it first
  358. */
  359. if( adapter->boot_ldrv_enabled ) {
  360. if( ldrv_num == 0 ) {
  361. ldrv_num = adapter->boot_ldrv;
  362. }
  363. else {
  364. if( ldrv_num <= adapter->boot_ldrv ) {
  365. ldrv_num--;
  366. }
  367. }
  368. }
  369. /*
  370. * If "delete logical drive" feature is enabled on this controller.
  371. * Do only if at least one delete logical drive operation was done.
  372. *
  373. * Also, after logical drive deletion, instead of logical drive number,
  374. * the value returned should be 0x80+logical drive id.
  375. *
  376. * These is valid only for IO commands.
  377. */
  378. if (adapter->support_random_del && adapter->read_ldidmap )
  379. switch (cmd->cmnd[0]) {
  380. case READ_6: /* fall through */
  381. case WRITE_6: /* fall through */
  382. case READ_10: /* fall through */
  383. case WRITE_10:
  384. ldrv_num += 0x80;
  385. }
  386. return ldrv_num;
  387. }
  388. /**
  389. * mega_build_cmd()
  390. * @adapter - pointer to our soft state
  391. * @cmd - Prepare using this scsi command
  392. * @busy - busy flag if no resources
  393. *
  394. * Prepares a command and scatter gather list for the controller. This routine
  395. * also finds out if the commands is intended for a logical drive or a
  396. * physical device and prepares the controller command accordingly.
  397. *
  398. * We also re-order the logical drives and physical devices based on their
  399. * boot settings.
  400. */
  401. static scb_t *
  402. mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
  403. {
  404. mega_ext_passthru *epthru;
  405. mega_passthru *pthru;
  406. scb_t *scb;
  407. mbox_t *mbox;
  408. long seg;
  409. char islogical;
  410. int max_ldrv_num;
  411. int channel = 0;
  412. int target = 0;
  413. int ldrv_num = 0; /* logical drive number */
  414. /*
  415. * filter the internal and ioctl commands
  416. */
  417. if((cmd->cmnd[0] == MEGA_INTERNAL_CMD)) {
  418. return cmd->buffer;
  419. }
  420. /*
  421. * We know what channels our logical drives are on - mega_find_card()
  422. */
  423. islogical = adapter->logdrv_chan[cmd->device->channel];
  424. /*
  425. * The theory: If physical drive is chosen for boot, all the physical
  426. * devices are exported before the logical drives, otherwise physical
  427. * devices are pushed after logical drives, in which case - Kernel sees
  428. * the physical devices on virtual channel which is obviously converted
  429. * to actual channel on the HBA.
  430. */
  431. if( adapter->boot_pdrv_enabled ) {
  432. if( islogical ) {
  433. /* logical channel */
  434. channel = cmd->device->channel -
  435. adapter->product_info.nchannels;
  436. }
  437. else {
  438. /* this is physical channel */
  439. channel = cmd->device->channel;
  440. target = cmd->device->id;
  441. /*
  442. * boot from a physical disk, that disk needs to be
  443. * exposed first IF both the channels are SCSI, then
  444. * booting from the second channel is not allowed.
  445. */
  446. if( target == 0 ) {
  447. target = adapter->boot_pdrv_tgt;
  448. }
  449. else if( target == adapter->boot_pdrv_tgt ) {
  450. target = 0;
  451. }
  452. }
  453. }
  454. else {
  455. if( islogical ) {
  456. /* this is the logical channel */
  457. channel = cmd->device->channel;
  458. }
  459. else {
  460. /* physical channel */
  461. channel = cmd->device->channel - NVIRT_CHAN;
  462. target = cmd->device->id;
  463. }
  464. }
  465. if(islogical) {
  466. /* have just LUN 0 for each target on virtual channels */
  467. if (cmd->device->lun) {
  468. cmd->result = (DID_BAD_TARGET << 16);
  469. cmd->scsi_done(cmd);
  470. return NULL;
  471. }
  472. ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
  473. max_ldrv_num = (adapter->flag & BOARD_40LD) ?
  474. MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
  475. /*
  476. * max_ldrv_num increases by 0x80 if some logical drive was
  477. * deleted.
  478. */
  479. if(adapter->read_ldidmap)
  480. max_ldrv_num += 0x80;
  481. if(ldrv_num > max_ldrv_num ) {
  482. cmd->result = (DID_BAD_TARGET << 16);
  483. cmd->scsi_done(cmd);
  484. return NULL;
  485. }
  486. }
  487. else {
  488. if( cmd->device->lun > 7) {
  489. /*
  490. * Do not support lun >7 for physically accessed
  491. * devices
  492. */
  493. cmd->result = (DID_BAD_TARGET << 16);
  494. cmd->scsi_done(cmd);
  495. return NULL;
  496. }
  497. }
  498. /*
  499. *
  500. * Logical drive commands
  501. *
  502. */
  503. if(islogical) {
  504. switch (cmd->cmnd[0]) {
  505. case TEST_UNIT_READY:
  506. #if MEGA_HAVE_CLUSTERING
  507. /*
  508. * Do we support clustering and is the support enabled
  509. * If no, return success always
  510. */
  511. if( !adapter->has_cluster ) {
  512. cmd->result = (DID_OK << 16);
  513. cmd->scsi_done(cmd);
  514. return NULL;
  515. }
  516. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  517. *busy = 1;
  518. return NULL;
  519. }
  520. scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
  521. scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
  522. scb->raw_mbox[3] = ldrv_num;
  523. scb->dma_direction = PCI_DMA_NONE;
  524. return scb;
  525. #else
  526. cmd->result = (DID_OK << 16);
  527. cmd->scsi_done(cmd);
  528. return NULL;
  529. #endif
  530. case MODE_SENSE: {
  531. char *buf;
  532. if (cmd->use_sg) {
  533. struct scatterlist *sg;
  534. sg = (struct scatterlist *)cmd->request_buffer;
  535. buf = kmap_atomic(sg->page, KM_IRQ0) +
  536. sg->offset;
  537. } else
  538. buf = cmd->request_buffer;
  539. memset(buf, 0, cmd->cmnd[4]);
  540. if (cmd->use_sg) {
  541. struct scatterlist *sg;
  542. sg = (struct scatterlist *)cmd->request_buffer;
  543. kunmap_atomic(buf - sg->offset, KM_IRQ0);
  544. }
  545. cmd->result = (DID_OK << 16);
  546. cmd->scsi_done(cmd);
  547. return NULL;
  548. }
  549. case READ_CAPACITY:
  550. case INQUIRY:
  551. if(!(adapter->flag & (1L << cmd->device->channel))) {
  552. printk(KERN_NOTICE
  553. "scsi%d: scanning scsi channel %d ",
  554. adapter->host->host_no,
  555. cmd->device->channel);
  556. printk("for logical drives.\n");
  557. adapter->flag |= (1L << cmd->device->channel);
  558. }
  559. /* Allocate a SCB and initialize passthru */
  560. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  561. *busy = 1;
  562. return NULL;
  563. }
  564. pthru = scb->pthru;
  565. mbox = (mbox_t *)scb->raw_mbox;
  566. memset(mbox, 0, sizeof(scb->raw_mbox));
  567. memset(pthru, 0, sizeof(mega_passthru));
  568. pthru->timeout = 0;
  569. pthru->ars = 1;
  570. pthru->reqsenselen = 14;
  571. pthru->islogical = 1;
  572. pthru->logdrv = ldrv_num;
  573. pthru->cdblen = cmd->cmd_len;
  574. memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
  575. if( adapter->has_64bit_addr ) {
  576. mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
  577. }
  578. else {
  579. mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
  580. }
  581. scb->dma_direction = PCI_DMA_FROMDEVICE;
  582. pthru->numsgelements = mega_build_sglist(adapter, scb,
  583. &pthru->dataxferaddr, &pthru->dataxferlen);
  584. mbox->m_out.xferaddr = scb->pthru_dma_addr;
  585. return scb;
  586. case READ_6:
  587. case WRITE_6:
  588. case READ_10:
  589. case WRITE_10:
  590. case READ_12:
  591. case WRITE_12:
  592. /* Allocate a SCB and initialize mailbox */
  593. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  594. *busy = 1;
  595. return NULL;
  596. }
  597. mbox = (mbox_t *)scb->raw_mbox;
  598. memset(mbox, 0, sizeof(scb->raw_mbox));
  599. mbox->m_out.logdrv = ldrv_num;
  600. /*
  601. * A little hack: 2nd bit is zero for all scsi read
  602. * commands and is set for all scsi write commands
  603. */
  604. if( adapter->has_64bit_addr ) {
  605. mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
  606. MEGA_MBOXCMD_LWRITE64:
  607. MEGA_MBOXCMD_LREAD64 ;
  608. }
  609. else {
  610. mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
  611. MEGA_MBOXCMD_LWRITE:
  612. MEGA_MBOXCMD_LREAD ;
  613. }
  614. /*
  615. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  616. */
  617. if( cmd->cmd_len == 6 ) {
  618. mbox->m_out.numsectors = (u32) cmd->cmnd[4];
  619. mbox->m_out.lba =
  620. ((u32)cmd->cmnd[1] << 16) |
  621. ((u32)cmd->cmnd[2] << 8) |
  622. (u32)cmd->cmnd[3];
  623. mbox->m_out.lba &= 0x1FFFFF;
  624. #if MEGA_HAVE_STATS
  625. /*
  626. * Take modulo 0x80, since the logical drive
  627. * number increases by 0x80 when a logical
  628. * drive was deleted
  629. */
  630. if (*cmd->cmnd == READ_6) {
  631. adapter->nreads[ldrv_num%0x80]++;
  632. adapter->nreadblocks[ldrv_num%0x80] +=
  633. mbox->m_out.numsectors;
  634. } else {
  635. adapter->nwrites[ldrv_num%0x80]++;
  636. adapter->nwriteblocks[ldrv_num%0x80] +=
  637. mbox->m_out.numsectors;
  638. }
  639. #endif
  640. }
  641. /*
  642. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  643. */
  644. if( cmd->cmd_len == 10 ) {
  645. mbox->m_out.numsectors =
  646. (u32)cmd->cmnd[8] |
  647. ((u32)cmd->cmnd[7] << 8);
  648. mbox->m_out.lba =
  649. ((u32)cmd->cmnd[2] << 24) |
  650. ((u32)cmd->cmnd[3] << 16) |
  651. ((u32)cmd->cmnd[4] << 8) |
  652. (u32)cmd->cmnd[5];
  653. #if MEGA_HAVE_STATS
  654. if (*cmd->cmnd == READ_10) {
  655. adapter->nreads[ldrv_num%0x80]++;
  656. adapter->nreadblocks[ldrv_num%0x80] +=
  657. mbox->m_out.numsectors;
  658. } else {
  659. adapter->nwrites[ldrv_num%0x80]++;
  660. adapter->nwriteblocks[ldrv_num%0x80] +=
  661. mbox->m_out.numsectors;
  662. }
  663. #endif
  664. }
  665. /*
  666. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  667. */
  668. if( cmd->cmd_len == 12 ) {
  669. mbox->m_out.lba =
  670. ((u32)cmd->cmnd[2] << 24) |
  671. ((u32)cmd->cmnd[3] << 16) |
  672. ((u32)cmd->cmnd[4] << 8) |
  673. (u32)cmd->cmnd[5];
  674. mbox->m_out.numsectors =
  675. ((u32)cmd->cmnd[6] << 24) |
  676. ((u32)cmd->cmnd[7] << 16) |
  677. ((u32)cmd->cmnd[8] << 8) |
  678. (u32)cmd->cmnd[9];
  679. #if MEGA_HAVE_STATS
  680. if (*cmd->cmnd == READ_12) {
  681. adapter->nreads[ldrv_num%0x80]++;
  682. adapter->nreadblocks[ldrv_num%0x80] +=
  683. mbox->m_out.numsectors;
  684. } else {
  685. adapter->nwrites[ldrv_num%0x80]++;
  686. adapter->nwriteblocks[ldrv_num%0x80] +=
  687. mbox->m_out.numsectors;
  688. }
  689. #endif
  690. }
  691. /*
  692. * If it is a read command
  693. */
  694. if( (*cmd->cmnd & 0x0F) == 0x08 ) {
  695. scb->dma_direction = PCI_DMA_FROMDEVICE;
  696. }
  697. else {
  698. scb->dma_direction = PCI_DMA_TODEVICE;
  699. }
  700. /* Calculate Scatter-Gather info */
  701. mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
  702. (u32 *)&mbox->m_out.xferaddr, (u32 *)&seg);
  703. return scb;
  704. #if MEGA_HAVE_CLUSTERING
  705. case RESERVE: /* Fall through */
  706. case RELEASE:
  707. /*
  708. * Do we support clustering and is the support enabled
  709. */
  710. if( ! adapter->has_cluster ) {
  711. cmd->result = (DID_BAD_TARGET << 16);
  712. cmd->scsi_done(cmd);
  713. return NULL;
  714. }
  715. /* Allocate a SCB and initialize mailbox */
  716. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  717. *busy = 1;
  718. return NULL;
  719. }
  720. scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
  721. scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
  722. MEGA_RESERVE_LD : MEGA_RELEASE_LD;
  723. scb->raw_mbox[3] = ldrv_num;
  724. scb->dma_direction = PCI_DMA_NONE;
  725. return scb;
  726. #endif
  727. default:
  728. cmd->result = (DID_BAD_TARGET << 16);
  729. cmd->scsi_done(cmd);
  730. return NULL;
  731. }
  732. }
  733. /*
  734. * Passthru drive commands
  735. */
  736. else {
  737. /* Allocate a SCB and initialize passthru */
  738. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  739. *busy = 1;
  740. return NULL;
  741. }
  742. mbox = (mbox_t *)scb->raw_mbox;
  743. memset(mbox, 0, sizeof(scb->raw_mbox));
  744. if( adapter->support_ext_cdb ) {
  745. epthru = mega_prepare_extpassthru(adapter, scb, cmd,
  746. channel, target);
  747. mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
  748. mbox->m_out.xferaddr = scb->epthru_dma_addr;
  749. }
  750. else {
  751. pthru = mega_prepare_passthru(adapter, scb, cmd,
  752. channel, target);
  753. /* Initialize mailbox */
  754. if( adapter->has_64bit_addr ) {
  755. mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
  756. }
  757. else {
  758. mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
  759. }
  760. mbox->m_out.xferaddr = scb->pthru_dma_addr;
  761. }
  762. return scb;
  763. }
  764. return NULL;
  765. }
  766. /**
  767. * mega_prepare_passthru()
  768. * @adapter - pointer to our soft state
  769. * @scb - our scsi control block
  770. * @cmd - scsi command from the mid-layer
  771. * @channel - actual channel on the controller
  772. * @target - actual id on the controller.
  773. *
  774. * prepare a command for the scsi physical devices.
  775. */
  776. static mega_passthru *
  777. mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
  778. int channel, int target)
  779. {
  780. mega_passthru *pthru;
  781. pthru = scb->pthru;
  782. memset(pthru, 0, sizeof (mega_passthru));
  783. /* 0=6sec/1=60sec/2=10min/3=3hrs */
  784. pthru->timeout = 2;
  785. pthru->ars = 1;
  786. pthru->reqsenselen = 14;
  787. pthru->islogical = 0;
  788. pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
  789. pthru->target = (adapter->flag & BOARD_40LD) ?
  790. (channel << 4) | target : target;
  791. pthru->cdblen = cmd->cmd_len;
  792. pthru->logdrv = cmd->device->lun;
  793. memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
  794. /* Not sure about the direction */
  795. scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
  796. /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
  797. switch (cmd->cmnd[0]) {
  798. case INQUIRY:
  799. case READ_CAPACITY:
  800. if(!(adapter->flag & (1L << cmd->device->channel))) {
  801. printk(KERN_NOTICE
  802. "scsi%d: scanning scsi channel %d [P%d] ",
  803. adapter->host->host_no,
  804. cmd->device->channel, channel);
  805. printk("for physical devices.\n");
  806. adapter->flag |= (1L << cmd->device->channel);
  807. }
  808. /* Fall through */
  809. default:
  810. pthru->numsgelements = mega_build_sglist(adapter, scb,
  811. &pthru->dataxferaddr, &pthru->dataxferlen);
  812. break;
  813. }
  814. return pthru;
  815. }
  816. /**
  817. * mega_prepare_extpassthru()
  818. * @adapter - pointer to our soft state
  819. * @scb - our scsi control block
  820. * @cmd - scsi command from the mid-layer
  821. * @channel - actual channel on the controller
  822. * @target - actual id on the controller.
  823. *
  824. * prepare a command for the scsi physical devices. This rountine prepares
  825. * commands for devices which can take extended CDBs (>10 bytes)
  826. */
  827. static mega_ext_passthru *
  828. mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
  829. int channel, int target)
  830. {
  831. mega_ext_passthru *epthru;
  832. epthru = scb->epthru;
  833. memset(epthru, 0, sizeof(mega_ext_passthru));
  834. /* 0=6sec/1=60sec/2=10min/3=3hrs */
  835. epthru->timeout = 2;
  836. epthru->ars = 1;
  837. epthru->reqsenselen = 14;
  838. epthru->islogical = 0;
  839. epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
  840. epthru->target = (adapter->flag & BOARD_40LD) ?
  841. (channel << 4) | target : target;
  842. epthru->cdblen = cmd->cmd_len;
  843. epthru->logdrv = cmd->device->lun;
  844. memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
  845. /* Not sure about the direction */
  846. scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
  847. switch(cmd->cmnd[0]) {
  848. case INQUIRY:
  849. case READ_CAPACITY:
  850. if(!(adapter->flag & (1L << cmd->device->channel))) {
  851. printk(KERN_NOTICE
  852. "scsi%d: scanning scsi channel %d [P%d] ",
  853. adapter->host->host_no,
  854. cmd->device->channel, channel);
  855. printk("for physical devices.\n");
  856. adapter->flag |= (1L << cmd->device->channel);
  857. }
  858. /* Fall through */
  859. default:
  860. epthru->numsgelements = mega_build_sglist(adapter, scb,
  861. &epthru->dataxferaddr, &epthru->dataxferlen);
  862. break;
  863. }
  864. return epthru;
  865. }
  866. static void
  867. __mega_runpendq(adapter_t *adapter)
  868. {
  869. scb_t *scb;
  870. struct list_head *pos, *next;
  871. /* Issue any pending commands to the card */
  872. list_for_each_safe(pos, next, &adapter->pending_list) {
  873. scb = list_entry(pos, scb_t, list);
  874. if( !(scb->state & SCB_ISSUED) ) {
  875. if( issue_scb(adapter, scb) != 0 )
  876. return;
  877. }
  878. }
  879. return;
  880. }
  881. /**
  882. * issue_scb()
  883. * @adapter - pointer to our soft state
  884. * @scb - scsi control block
  885. *
  886. * Post a command to the card if the mailbox is available, otherwise return
  887. * busy. We also take the scb from the pending list if the mailbox is
  888. * available.
  889. */
  890. static int
  891. issue_scb(adapter_t *adapter, scb_t *scb)
  892. {
  893. volatile mbox64_t *mbox64 = adapter->mbox64;
  894. volatile mbox_t *mbox = adapter->mbox;
  895. unsigned int i = 0;
  896. if(unlikely(mbox->m_in.busy)) {
  897. do {
  898. udelay(1);
  899. i++;
  900. } while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
  901. if(mbox->m_in.busy) return -1;
  902. }
  903. /* Copy mailbox data into host structure */
  904. memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox,
  905. sizeof(struct mbox_out));
  906. mbox->m_out.cmdid = scb->idx; /* Set cmdid */
  907. mbox->m_in.busy = 1; /* Set busy */
  908. /*
  909. * Increment the pending queue counter
  910. */
  911. atomic_inc(&adapter->pend_cmds);
  912. switch (mbox->m_out.cmd) {
  913. case MEGA_MBOXCMD_LREAD64:
  914. case MEGA_MBOXCMD_LWRITE64:
  915. case MEGA_MBOXCMD_PASSTHRU64:
  916. case MEGA_MBOXCMD_EXTPTHRU:
  917. mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
  918. mbox64->xfer_segment_hi = 0;
  919. mbox->m_out.xferaddr = 0xFFFFFFFF;
  920. break;
  921. default:
  922. mbox64->xfer_segment_lo = 0;
  923. mbox64->xfer_segment_hi = 0;
  924. }
  925. /*
  926. * post the command
  927. */
  928. scb->state |= SCB_ISSUED;
  929. if( likely(adapter->flag & BOARD_MEMMAP) ) {
  930. mbox->m_in.poll = 0;
  931. mbox->m_in.ack = 0;
  932. WRINDOOR(adapter, adapter->mbox_dma | 0x1);
  933. }
  934. else {
  935. irq_enable(adapter);
  936. issue_command(adapter);
  937. }
  938. return 0;
  939. }
  940. /*
  941. * Wait until the controller's mailbox is available
  942. */
  943. static inline int
  944. mega_busywait_mbox (adapter_t *adapter)
  945. {
  946. if (adapter->mbox->m_in.busy)
  947. return __mega_busywait_mbox(adapter);
  948. return 0;
  949. }
  950. /**
  951. * issue_scb_block()
  952. * @adapter - pointer to our soft state
  953. * @raw_mbox - the mailbox
  954. *
  955. * Issue a scb in synchronous and non-interrupt mode
  956. */
  957. static int
  958. issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
  959. {
  960. volatile mbox64_t *mbox64 = adapter->mbox64;
  961. volatile mbox_t *mbox = adapter->mbox;
  962. u8 byte;
  963. /* Wait until mailbox is free */
  964. if(mega_busywait_mbox (adapter))
  965. goto bug_blocked_mailbox;
  966. /* Copy mailbox data into host structure */
  967. memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
  968. mbox->m_out.cmdid = 0xFE;
  969. mbox->m_in.busy = 1;
  970. switch (raw_mbox[0]) {
  971. case MEGA_MBOXCMD_LREAD64:
  972. case MEGA_MBOXCMD_LWRITE64:
  973. case MEGA_MBOXCMD_PASSTHRU64:
  974. case MEGA_MBOXCMD_EXTPTHRU:
  975. mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
  976. mbox64->xfer_segment_hi = 0;
  977. mbox->m_out.xferaddr = 0xFFFFFFFF;
  978. break;
  979. default:
  980. mbox64->xfer_segment_lo = 0;
  981. mbox64->xfer_segment_hi = 0;
  982. }
  983. if( likely(adapter->flag & BOARD_MEMMAP) ) {
  984. mbox->m_in.poll = 0;
  985. mbox->m_in.ack = 0;
  986. mbox->m_in.numstatus = 0xFF;
  987. mbox->m_in.status = 0xFF;
  988. WRINDOOR(adapter, adapter->mbox_dma | 0x1);
  989. while((volatile u8)mbox->m_in.numstatus == 0xFF)
  990. cpu_relax();
  991. mbox->m_in.numstatus = 0xFF;
  992. while( (volatile u8)mbox->m_in.poll != 0x77 )
  993. cpu_relax();
  994. mbox->m_in.poll = 0;
  995. mbox->m_in.ack = 0x77;
  996. WRINDOOR(adapter, adapter->mbox_dma | 0x2);
  997. while(RDINDOOR(adapter) & 0x2)
  998. cpu_relax();
  999. }
  1000. else {
  1001. irq_disable(adapter);
  1002. issue_command(adapter);
  1003. while (!((byte = irq_state(adapter)) & INTR_VALID))
  1004. cpu_relax();
  1005. set_irq_state(adapter, byte);
  1006. irq_enable(adapter);
  1007. irq_ack(adapter);
  1008. }
  1009. return mbox->m_in.status;
  1010. bug_blocked_mailbox:
  1011. printk(KERN_WARNING "megaraid: Blocked mailbox......!!\n");
  1012. udelay (1000);
  1013. return -1;
  1014. }
  1015. /**
  1016. * megaraid_isr_iomapped()
  1017. * @irq - irq
  1018. * @devp - pointer to our soft state
  1019. * @regs - unused
  1020. *
  1021. * Interrupt service routine for io-mapped controllers.
  1022. * Find out if our device is interrupting. If yes, acknowledge the interrupt
  1023. * and service the completed commands.
  1024. */
  1025. static irqreturn_t
  1026. megaraid_isr_iomapped(int irq, void *devp, struct pt_regs *regs)
  1027. {
  1028. adapter_t *adapter = devp;
  1029. unsigned long flags;
  1030. u8 status;
  1031. u8 nstatus;
  1032. u8 completed[MAX_FIRMWARE_STATUS];
  1033. u8 byte;
  1034. int handled = 0;
  1035. /*
  1036. * loop till F/W has more commands for us to complete.
  1037. */
  1038. spin_lock_irqsave(&adapter->lock, flags);
  1039. do {
  1040. /* Check if a valid interrupt is pending */
  1041. byte = irq_state(adapter);
  1042. if( (byte & VALID_INTR_BYTE) == 0 ) {
  1043. /*
  1044. * No more pending commands
  1045. */
  1046. goto out_unlock;
  1047. }
  1048. set_irq_state(adapter, byte);
  1049. while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
  1050. == 0xFF)
  1051. cpu_relax();
  1052. adapter->mbox->m_in.numstatus = 0xFF;
  1053. status = adapter->mbox->m_in.status;
  1054. /*
  1055. * decrement the pending queue counter
  1056. */
  1057. atomic_sub(nstatus, &adapter->pend_cmds);
  1058. memcpy(completed, (void *)adapter->mbox->m_in.completed,
  1059. nstatus);
  1060. /* Acknowledge interrupt */
  1061. irq_ack(adapter);
  1062. mega_cmd_done(adapter, completed, nstatus, status);
  1063. mega_rundoneq(adapter);
  1064. handled = 1;
  1065. /* Loop through any pending requests */
  1066. if(atomic_read(&adapter->quiescent) == 0) {
  1067. mega_runpendq(adapter);
  1068. }
  1069. } while(1);
  1070. out_unlock:
  1071. spin_unlock_irqrestore(&adapter->lock, flags);
  1072. return IRQ_RETVAL(handled);
  1073. }
  1074. /**
  1075. * megaraid_isr_memmapped()
  1076. * @irq - irq
  1077. * @devp - pointer to our soft state
  1078. * @regs - unused
  1079. *
  1080. * Interrupt service routine for memory-mapped controllers.
  1081. * Find out if our device is interrupting. If yes, acknowledge the interrupt
  1082. * and service the completed commands.
  1083. */
  1084. static irqreturn_t
  1085. megaraid_isr_memmapped(int irq, void *devp, struct pt_regs *regs)
  1086. {
  1087. adapter_t *adapter = devp;
  1088. unsigned long flags;
  1089. u8 status;
  1090. u32 dword = 0;
  1091. u8 nstatus;
  1092. u8 completed[MAX_FIRMWARE_STATUS];
  1093. int handled = 0;
  1094. /*
  1095. * loop till F/W has more commands for us to complete.
  1096. */
  1097. spin_lock_irqsave(&adapter->lock, flags);
  1098. do {
  1099. /* Check if a valid interrupt is pending */
  1100. dword = RDOUTDOOR(adapter);
  1101. if(dword != 0x10001234) {
  1102. /*
  1103. * No more pending commands
  1104. */
  1105. goto out_unlock;
  1106. }
  1107. WROUTDOOR(adapter, 0x10001234);
  1108. while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
  1109. == 0xFF) {
  1110. cpu_relax();
  1111. }
  1112. adapter->mbox->m_in.numstatus = 0xFF;
  1113. status = adapter->mbox->m_in.status;
  1114. /*
  1115. * decrement the pending queue counter
  1116. */
  1117. atomic_sub(nstatus, &adapter->pend_cmds);
  1118. memcpy(completed, (void *)adapter->mbox->m_in.completed,
  1119. nstatus);
  1120. /* Acknowledge interrupt */
  1121. WRINDOOR(adapter, 0x2);
  1122. handled = 1;
  1123. while( RDINDOOR(adapter) & 0x02 ) cpu_relax();
  1124. mega_cmd_done(adapter, completed, nstatus, status);
  1125. mega_rundoneq(adapter);
  1126. /* Loop through any pending requests */
  1127. if(atomic_read(&adapter->quiescent) == 0) {
  1128. mega_runpendq(adapter);
  1129. }
  1130. } while(1);
  1131. out_unlock:
  1132. spin_unlock_irqrestore(&adapter->lock, flags);
  1133. return IRQ_RETVAL(handled);
  1134. }
  1135. /**
  1136. * mega_cmd_done()
  1137. * @adapter - pointer to our soft state
  1138. * @completed - array of ids of completed commands
  1139. * @nstatus - number of completed commands
  1140. * @status - status of the last command completed
  1141. *
  1142. * Complete the comamnds and call the scsi mid-layer callback hooks.
  1143. */
  1144. static void
  1145. mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
  1146. {
  1147. mega_ext_passthru *epthru = NULL;
  1148. struct scatterlist *sgl;
  1149. Scsi_Cmnd *cmd = NULL;
  1150. mega_passthru *pthru = NULL;
  1151. mbox_t *mbox = NULL;
  1152. u8 c;
  1153. scb_t *scb;
  1154. int islogical;
  1155. int cmdid;
  1156. int i;
  1157. /*
  1158. * for all the commands completed, call the mid-layer callback routine
  1159. * and free the scb.
  1160. */
  1161. for( i = 0; i < nstatus; i++ ) {
  1162. cmdid = completed[i];
  1163. if( cmdid == CMDID_INT_CMDS ) { /* internal command */
  1164. scb = &adapter->int_scb;
  1165. cmd = scb->cmd;
  1166. mbox = (mbox_t *)scb->raw_mbox;
  1167. /*
  1168. * Internal command interface do not fire the extended
  1169. * passthru or 64-bit passthru
  1170. */
  1171. pthru = scb->pthru;
  1172. }
  1173. else {
  1174. scb = &adapter->scb_list[cmdid];
  1175. /*
  1176. * Make sure f/w has completed a valid command
  1177. */
  1178. if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
  1179. printk(KERN_CRIT
  1180. "megaraid: invalid command ");
  1181. printk("Id %d, scb->state:%x, scsi cmd:%p\n",
  1182. cmdid, scb->state, scb->cmd);
  1183. continue;
  1184. }
  1185. /*
  1186. * Was a abort issued for this command
  1187. */
  1188. if( scb->state & SCB_ABORT ) {
  1189. printk(KERN_WARNING
  1190. "megaraid: aborted cmd %lx[%x] complete.\n",
  1191. scb->cmd->serial_number, scb->idx);
  1192. scb->cmd->result = (DID_ABORT << 16);
  1193. list_add_tail(SCSI_LIST(scb->cmd),
  1194. &adapter->completed_list);
  1195. mega_free_scb(adapter, scb);
  1196. continue;
  1197. }
  1198. /*
  1199. * Was a reset issued for this command
  1200. */
  1201. if( scb->state & SCB_RESET ) {
  1202. printk(KERN_WARNING
  1203. "megaraid: reset cmd %lx[%x] complete.\n",
  1204. scb->cmd->serial_number, scb->idx);
  1205. scb->cmd->result = (DID_RESET << 16);
  1206. list_add_tail(SCSI_LIST(scb->cmd),
  1207. &adapter->completed_list);
  1208. mega_free_scb (adapter, scb);
  1209. continue;
  1210. }
  1211. cmd = scb->cmd;
  1212. pthru = scb->pthru;
  1213. epthru = scb->epthru;
  1214. mbox = (mbox_t *)scb->raw_mbox;
  1215. #if MEGA_HAVE_STATS
  1216. {
  1217. int logdrv = mbox->m_out.logdrv;
  1218. islogical = adapter->logdrv_chan[cmd->channel];
  1219. /*
  1220. * Maintain an error counter for the logical drive.
  1221. * Some application like SNMP agent need such
  1222. * statistics
  1223. */
  1224. if( status && islogical && (cmd->cmnd[0] == READ_6 ||
  1225. cmd->cmnd[0] == READ_10 ||
  1226. cmd->cmnd[0] == READ_12)) {
  1227. /*
  1228. * Logical drive number increases by 0x80 when
  1229. * a logical drive is deleted
  1230. */
  1231. adapter->rd_errors[logdrv%0x80]++;
  1232. }
  1233. if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
  1234. cmd->cmnd[0] == WRITE_10 ||
  1235. cmd->cmnd[0] == WRITE_12)) {
  1236. /*
  1237. * Logical drive number increases by 0x80 when
  1238. * a logical drive is deleted
  1239. */
  1240. adapter->wr_errors[logdrv%0x80]++;
  1241. }
  1242. }
  1243. #endif
  1244. }
  1245. /*
  1246. * Do not return the presence of hard disk on the channel so,
  1247. * inquiry sent, and returned data==hard disk or removable
  1248. * hard disk and not logical, request should return failure! -
  1249. * PJ
  1250. */
  1251. islogical = adapter->logdrv_chan[cmd->device->channel];
  1252. if( cmd->cmnd[0] == INQUIRY && !islogical ) {
  1253. if( cmd->use_sg ) {
  1254. sgl = (struct scatterlist *)
  1255. cmd->request_buffer;
  1256. if( sgl->page ) {
  1257. c = *(unsigned char *)
  1258. page_address((&sgl[0])->page) +
  1259. (&sgl[0])->offset;
  1260. }
  1261. else {
  1262. printk(KERN_WARNING
  1263. "megaraid: invalid sg.\n");
  1264. c = 0;
  1265. }
  1266. }
  1267. else {
  1268. c = *(u8 *)cmd->request_buffer;
  1269. }
  1270. if(IS_RAID_CH(adapter, cmd->device->channel) &&
  1271. ((c & 0x1F ) == TYPE_DISK)) {
  1272. status = 0xF0;
  1273. }
  1274. }
  1275. /* clear result; otherwise, success returns corrupt value */
  1276. cmd->result = 0;
  1277. /* Convert MegaRAID status to Linux error code */
  1278. switch (status) {
  1279. case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
  1280. cmd->result |= (DID_OK << 16);
  1281. break;
  1282. case 0x02: /* ERROR_ABORTED, i.e.
  1283. SCSI_STATUS_CHECK_CONDITION */
  1284. /* set sense_buffer and result fields */
  1285. if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
  1286. mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
  1287. memcpy(cmd->sense_buffer, pthru->reqsensearea,
  1288. 14);
  1289. cmd->result = (DRIVER_SENSE << 24) |
  1290. (DID_OK << 16) |
  1291. (CHECK_CONDITION << 1);
  1292. }
  1293. else {
  1294. if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
  1295. memcpy(cmd->sense_buffer,
  1296. epthru->reqsensearea, 14);
  1297. cmd->result = (DRIVER_SENSE << 24) |
  1298. (DID_OK << 16) |
  1299. (CHECK_CONDITION << 1);
  1300. } else {
  1301. cmd->sense_buffer[0] = 0x70;
  1302. cmd->sense_buffer[2] = ABORTED_COMMAND;
  1303. cmd->result |= (CHECK_CONDITION << 1);
  1304. }
  1305. }
  1306. break;
  1307. case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
  1308. SCSI_STATUS_BUSY */
  1309. cmd->result |= (DID_BUS_BUSY << 16) | status;
  1310. break;
  1311. default:
  1312. #if MEGA_HAVE_CLUSTERING
  1313. /*
  1314. * If TEST_UNIT_READY fails, we know
  1315. * MEGA_RESERVATION_STATUS failed
  1316. */
  1317. if( cmd->cmnd[0] == TEST_UNIT_READY ) {
  1318. cmd->result |= (DID_ERROR << 16) |
  1319. (RESERVATION_CONFLICT << 1);
  1320. }
  1321. else
  1322. /*
  1323. * Error code returned is 1 if Reserve or Release
  1324. * failed or the input parameter is invalid
  1325. */
  1326. if( status == 1 &&
  1327. (cmd->cmnd[0] == RESERVE ||
  1328. cmd->cmnd[0] == RELEASE) ) {
  1329. cmd->result |= (DID_ERROR << 16) |
  1330. (RESERVATION_CONFLICT << 1);
  1331. }
  1332. else
  1333. #endif
  1334. cmd->result |= (DID_BAD_TARGET << 16)|status;
  1335. }
  1336. /*
  1337. * Only free SCBs for the commands coming down from the
  1338. * mid-layer, not for which were issued internally
  1339. *
  1340. * For internal command, restore the status returned by the
  1341. * firmware so that user can interpret it.
  1342. */
  1343. if( cmdid == CMDID_INT_CMDS ) { /* internal command */
  1344. cmd->result = status;
  1345. /*
  1346. * Remove the internal command from the pending list
  1347. */
  1348. list_del_init(&scb->list);
  1349. scb->state = SCB_FREE;
  1350. }
  1351. else {
  1352. mega_free_scb(adapter, scb);
  1353. }
  1354. /* Add Scsi_Command to end of completed queue */
  1355. list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
  1356. }
  1357. }
  1358. /*
  1359. * mega_runpendq()
  1360. *
  1361. * Run through the list of completed requests and finish it
  1362. */
  1363. static void
  1364. mega_rundoneq (adapter_t *adapter)
  1365. {
  1366. Scsi_Cmnd *cmd;
  1367. struct list_head *pos;
  1368. list_for_each(pos, &adapter->completed_list) {
  1369. struct scsi_pointer* spos = (struct scsi_pointer *)pos;
  1370. cmd = list_entry(spos, Scsi_Cmnd, SCp);
  1371. cmd->scsi_done(cmd);
  1372. }
  1373. INIT_LIST_HEAD(&adapter->completed_list);
  1374. }
  1375. /*
  1376. * Free a SCB structure
  1377. * Note: We assume the scsi commands associated with this scb is not free yet.
  1378. */
  1379. static void
  1380. mega_free_scb(adapter_t *adapter, scb_t *scb)
  1381. {
  1382. unsigned long length;
  1383. switch( scb->dma_type ) {
  1384. case MEGA_DMA_TYPE_NONE:
  1385. break;
  1386. case MEGA_BULK_DATA:
  1387. if (scb->cmd->use_sg == 0)
  1388. length = scb->cmd->request_bufflen;
  1389. else {
  1390. struct scatterlist *sgl =
  1391. (struct scatterlist *)scb->cmd->request_buffer;
  1392. length = sgl->length;
  1393. }
  1394. pci_unmap_page(adapter->dev, scb->dma_h_bulkdata,
  1395. length, scb->dma_direction);
  1396. break;
  1397. case MEGA_SGLIST:
  1398. pci_unmap_sg(adapter->dev, scb->cmd->request_buffer,
  1399. scb->cmd->use_sg, scb->dma_direction);
  1400. break;
  1401. default:
  1402. break;
  1403. }
  1404. /*
  1405. * Remove from the pending list
  1406. */
  1407. list_del_init(&scb->list);
  1408. /* Link the scb back into free list */
  1409. scb->state = SCB_FREE;
  1410. scb->cmd = NULL;
  1411. list_add(&scb->list, &adapter->free_list);
  1412. }
  1413. static int
  1414. __mega_busywait_mbox (adapter_t *adapter)
  1415. {
  1416. volatile mbox_t *mbox = adapter->mbox;
  1417. long counter;
  1418. for (counter = 0; counter < 10000; counter++) {
  1419. if (!mbox->m_in.busy)
  1420. return 0;
  1421. udelay(100); yield();
  1422. }
  1423. return -1; /* give up after 1 second */
  1424. }
  1425. /*
  1426. * Copies data to SGLIST
  1427. * Note: For 64 bit cards, we need a minimum of one SG element for read/write
  1428. */
  1429. static int
  1430. mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
  1431. {
  1432. struct scatterlist *sgl;
  1433. struct page *page;
  1434. unsigned long offset;
  1435. unsigned int length;
  1436. Scsi_Cmnd *cmd;
  1437. int sgcnt;
  1438. int idx;
  1439. cmd = scb->cmd;
  1440. /* Scatter-gather not used */
  1441. if( cmd->use_sg == 0 || (cmd->use_sg == 1 &&
  1442. !adapter->has_64bit_addr)) {
  1443. if (cmd->use_sg == 0) {
  1444. page = virt_to_page(cmd->request_buffer);
  1445. offset = offset_in_page(cmd->request_buffer);
  1446. length = cmd->request_bufflen;
  1447. } else {
  1448. sgl = (struct scatterlist *)cmd->request_buffer;
  1449. page = sgl->page;
  1450. offset = sgl->offset;
  1451. length = sgl->length;
  1452. }
  1453. scb->dma_h_bulkdata = pci_map_page(adapter->dev,
  1454. page, offset,
  1455. length,
  1456. scb->dma_direction);
  1457. scb->dma_type = MEGA_BULK_DATA;
  1458. /*
  1459. * We need to handle special 64-bit commands that need a
  1460. * minimum of 1 SG
  1461. */
  1462. if( adapter->has_64bit_addr ) {
  1463. scb->sgl64[0].address = scb->dma_h_bulkdata;
  1464. scb->sgl64[0].length = length;
  1465. *buf = (u32)scb->sgl_dma_addr;
  1466. *len = (u32)length;
  1467. return 1;
  1468. }
  1469. else {
  1470. *buf = (u32)scb->dma_h_bulkdata;
  1471. *len = (u32)length;
  1472. }
  1473. return 0;
  1474. }
  1475. sgl = (struct scatterlist *)cmd->request_buffer;
  1476. /*
  1477. * Copy Scatter-Gather list info into controller structure.
  1478. *
  1479. * The number of sg elements returned must not exceed our limit
  1480. */
  1481. sgcnt = pci_map_sg(adapter->dev, sgl, cmd->use_sg,
  1482. scb->dma_direction);
  1483. scb->dma_type = MEGA_SGLIST;
  1484. if( sgcnt > adapter->sglen ) BUG();
  1485. *len = 0;
  1486. for( idx = 0; idx < sgcnt; idx++, sgl++ ) {
  1487. if( adapter->has_64bit_addr ) {
  1488. scb->sgl64[idx].address = sg_dma_address(sgl);
  1489. *len += scb->sgl64[idx].length = sg_dma_len(sgl);
  1490. }
  1491. else {
  1492. scb->sgl[idx].address = sg_dma_address(sgl);
  1493. *len += scb->sgl[idx].length = sg_dma_len(sgl);
  1494. }
  1495. }
  1496. /* Reset pointer and length fields */
  1497. *buf = scb->sgl_dma_addr;
  1498. /* Return count of SG requests */
  1499. return sgcnt;
  1500. }
  1501. /*
  1502. * mega_8_to_40ld()
  1503. *
  1504. * takes all info in AdapterInquiry structure and puts it into ProductInfo and
  1505. * Enquiry3 structures for later use
  1506. */
  1507. static void
  1508. mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
  1509. mega_product_info *product_info)
  1510. {
  1511. int i;
  1512. product_info->max_commands = inquiry->adapter_info.max_commands;
  1513. enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
  1514. product_info->nchannels = inquiry->adapter_info.nchannels;
  1515. for (i = 0; i < 4; i++) {
  1516. product_info->fw_version[i] =
  1517. inquiry->adapter_info.fw_version[i];
  1518. product_info->bios_version[i] =
  1519. inquiry->adapter_info.bios_version[i];
  1520. }
  1521. enquiry3->cache_flush_interval =
  1522. inquiry->adapter_info.cache_flush_interval;
  1523. product_info->dram_size = inquiry->adapter_info.dram_size;
  1524. enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
  1525. for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
  1526. enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
  1527. enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
  1528. enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
  1529. }
  1530. for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
  1531. enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
  1532. }
  1533. static inline void
  1534. mega_free_sgl(adapter_t *adapter)
  1535. {
  1536. scb_t *scb;
  1537. int i;
  1538. for(i = 0; i < adapter->max_cmds; i++) {
  1539. scb = &adapter->scb_list[i];
  1540. if( scb->sgl64 ) {
  1541. pci_free_consistent(adapter->dev,
  1542. sizeof(mega_sgl64) * adapter->sglen,
  1543. scb->sgl64,
  1544. scb->sgl_dma_addr);
  1545. scb->sgl64 = NULL;
  1546. }
  1547. if( scb->pthru ) {
  1548. pci_free_consistent(adapter->dev, sizeof(mega_passthru),
  1549. scb->pthru, scb->pthru_dma_addr);
  1550. scb->pthru = NULL;
  1551. }
  1552. if( scb->epthru ) {
  1553. pci_free_consistent(adapter->dev,
  1554. sizeof(mega_ext_passthru),
  1555. scb->epthru, scb->epthru_dma_addr);
  1556. scb->epthru = NULL;
  1557. }
  1558. }
  1559. }
  1560. /*
  1561. * Get information about the card/driver
  1562. */
  1563. const char *
  1564. megaraid_info(struct Scsi_Host *host)
  1565. {
  1566. static char buffer[512];
  1567. adapter_t *adapter;
  1568. adapter = (adapter_t *)host->hostdata;
  1569. sprintf (buffer,
  1570. "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
  1571. adapter->fw_version, adapter->product_info.max_commands,
  1572. adapter->host->max_id, adapter->host->max_channel,
  1573. adapter->host->max_lun);
  1574. return buffer;
  1575. }
  1576. /*
  1577. * Abort a previous SCSI request. Only commands on the pending list can be
  1578. * aborted. All the commands issued to the F/W must complete.
  1579. */
  1580. static int
  1581. megaraid_abort(Scsi_Cmnd *cmd)
  1582. {
  1583. adapter_t *adapter;
  1584. int rval;
  1585. adapter = (adapter_t *)cmd->device->host->hostdata;
  1586. rval = megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
  1587. /*
  1588. * This is required here to complete any completed requests
  1589. * to be communicated over to the mid layer.
  1590. */
  1591. mega_rundoneq(adapter);
  1592. return rval;
  1593. }
  1594. static int
  1595. megaraid_reset(struct scsi_cmnd *cmd)
  1596. {
  1597. adapter_t *adapter;
  1598. megacmd_t mc;
  1599. int rval;
  1600. adapter = (adapter_t *)cmd->device->host->hostdata;
  1601. #if MEGA_HAVE_CLUSTERING
  1602. mc.cmd = MEGA_CLUSTER_CMD;
  1603. mc.opcode = MEGA_RESET_RESERVATIONS;
  1604. if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
  1605. printk(KERN_WARNING
  1606. "megaraid: reservation reset failed.\n");
  1607. }
  1608. else {
  1609. printk(KERN_INFO "megaraid: reservation reset.\n");
  1610. }
  1611. #endif
  1612. spin_lock_irq(&adapter->lock);
  1613. rval = megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
  1614. /*
  1615. * This is required here to complete any completed requests
  1616. * to be communicated over to the mid layer.
  1617. */
  1618. mega_rundoneq(adapter);
  1619. spin_unlock_irq(&adapter->lock);
  1620. return rval;
  1621. }
  1622. /**
  1623. * megaraid_abort_and_reset()
  1624. * @adapter - megaraid soft state
  1625. * @cmd - scsi command to be aborted or reset
  1626. * @aor - abort or reset flag
  1627. *
  1628. * Try to locate the scsi command in the pending queue. If found and is not
  1629. * issued to the controller, abort/reset it. Otherwise return failure
  1630. */
  1631. static int
  1632. megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
  1633. {
  1634. struct list_head *pos, *next;
  1635. scb_t *scb;
  1636. printk(KERN_WARNING "megaraid: %s-%lx cmd=%x <c=%d t=%d l=%d>\n",
  1637. (aor == SCB_ABORT)? "ABORTING":"RESET", cmd->serial_number,
  1638. cmd->cmnd[0], cmd->device->channel,
  1639. cmd->device->id, cmd->device->lun);
  1640. if(list_empty(&adapter->pending_list))
  1641. return FALSE;
  1642. list_for_each_safe(pos, next, &adapter->pending_list) {
  1643. scb = list_entry(pos, scb_t, list);
  1644. if (scb->cmd == cmd) { /* Found command */
  1645. scb->state |= aor;
  1646. /*
  1647. * Check if this command has firmare owenership. If
  1648. * yes, we cannot reset this command. Whenever, f/w
  1649. * completes this command, we will return appropriate
  1650. * status from ISR.
  1651. */
  1652. if( scb->state & SCB_ISSUED ) {
  1653. printk(KERN_WARNING
  1654. "megaraid: %s-%lx[%x], fw owner.\n",
  1655. (aor==SCB_ABORT) ? "ABORTING":"RESET",
  1656. cmd->serial_number, scb->idx);
  1657. return FALSE;
  1658. }
  1659. else {
  1660. /*
  1661. * Not yet issued! Remove from the pending
  1662. * list
  1663. */
  1664. printk(KERN_WARNING
  1665. "megaraid: %s-%lx[%x], driver owner.\n",
  1666. (aor==SCB_ABORT) ? "ABORTING":"RESET",
  1667. cmd->serial_number, scb->idx);
  1668. mega_free_scb(adapter, scb);
  1669. if( aor == SCB_ABORT ) {
  1670. cmd->result = (DID_ABORT << 16);
  1671. }
  1672. else {
  1673. cmd->result = (DID_RESET << 16);
  1674. }
  1675. list_add_tail(SCSI_LIST(cmd),
  1676. &adapter->completed_list);
  1677. return TRUE;
  1678. }
  1679. }
  1680. }
  1681. return FALSE;
  1682. }
  1683. static inline int
  1684. make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
  1685. {
  1686. *pdev = kmalloc(sizeof(struct pci_dev), GFP_KERNEL);
  1687. if( *pdev == NULL ) return -1;
  1688. memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
  1689. if( pci_set_dma_mask(*pdev, DMA_32BIT_MASK) != 0 ) {
  1690. kfree(*pdev);
  1691. return -1;
  1692. }
  1693. return 0;
  1694. }
  1695. static inline void
  1696. free_local_pdev(struct pci_dev *pdev)
  1697. {
  1698. kfree(pdev);
  1699. }
  1700. /**
  1701. * mega_allocate_inquiry()
  1702. * @dma_handle - handle returned for dma address
  1703. * @pdev - handle to pci device
  1704. *
  1705. * allocates memory for inquiry structure
  1706. */
  1707. static inline void *
  1708. mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
  1709. {
  1710. return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
  1711. }
  1712. static inline void
  1713. mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
  1714. {
  1715. pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
  1716. }
  1717. #ifdef CONFIG_PROC_FS
  1718. /* Following code handles /proc fs */
  1719. #define CREATE_READ_PROC(string, func) create_proc_read_entry(string, \
  1720. S_IRUSR | S_IFREG, \
  1721. controller_proc_dir_entry, \
  1722. func, adapter)
  1723. /**
  1724. * mega_create_proc_entry()
  1725. * @index - index in soft state array
  1726. * @parent - parent node for this /proc entry
  1727. *
  1728. * Creates /proc entries for our controllers.
  1729. */
  1730. static void
  1731. mega_create_proc_entry(int index, struct proc_dir_entry *parent)
  1732. {
  1733. struct proc_dir_entry *controller_proc_dir_entry = NULL;
  1734. u8 string[64] = { 0 };
  1735. adapter_t *adapter = hba_soft_state[index];
  1736. sprintf(string, "hba%d", adapter->host->host_no);
  1737. controller_proc_dir_entry =
  1738. adapter->controller_proc_dir_entry = proc_mkdir(string, parent);
  1739. if(!controller_proc_dir_entry) {
  1740. printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
  1741. return;
  1742. }
  1743. adapter->proc_read = CREATE_READ_PROC("config", proc_read_config);
  1744. adapter->proc_stat = CREATE_READ_PROC("stat", proc_read_stat);
  1745. adapter->proc_mbox = CREATE_READ_PROC("mailbox", proc_read_mbox);
  1746. #if MEGA_HAVE_ENH_PROC
  1747. adapter->proc_rr = CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate);
  1748. adapter->proc_battery = CREATE_READ_PROC("battery-status",
  1749. proc_battery);
  1750. /*
  1751. * Display each physical drive on its channel
  1752. */
  1753. adapter->proc_pdrvstat[0] = CREATE_READ_PROC("diskdrives-ch0",
  1754. proc_pdrv_ch0);
  1755. adapter->proc_pdrvstat[1] = CREATE_READ_PROC("diskdrives-ch1",
  1756. proc_pdrv_ch1);
  1757. adapter->proc_pdrvstat[2] = CREATE_READ_PROC("diskdrives-ch2",
  1758. proc_pdrv_ch2);
  1759. adapter->proc_pdrvstat[3] = CREATE_READ_PROC("diskdrives-ch3",
  1760. proc_pdrv_ch3);
  1761. /*
  1762. * Display a set of up to 10 logical drive through each of following
  1763. * /proc entries
  1764. */
  1765. adapter->proc_rdrvstat[0] = CREATE_READ_PROC("raiddrives-0-9",
  1766. proc_rdrv_10);
  1767. adapter->proc_rdrvstat[1] = CREATE_READ_PROC("raiddrives-10-19",
  1768. proc_rdrv_20);
  1769. adapter->proc_rdrvstat[2] = CREATE_READ_PROC("raiddrives-20-29",
  1770. proc_rdrv_30);
  1771. adapter->proc_rdrvstat[3] = CREATE_READ_PROC("raiddrives-30-39",
  1772. proc_rdrv_40);
  1773. #endif
  1774. }
  1775. /**
  1776. * proc_read_config()
  1777. * @page - buffer to write the data in
  1778. * @start - where the actual data has been written in page
  1779. * @offset - same meaning as the read system call
  1780. * @count - same meaning as the read system call
  1781. * @eof - set if no more data needs to be returned
  1782. * @data - pointer to our soft state
  1783. *
  1784. * Display configuration information about the controller.
  1785. */
  1786. static int
  1787. proc_read_config(char *page, char **start, off_t offset, int count, int *eof,
  1788. void *data)
  1789. {
  1790. adapter_t *adapter = (adapter_t *)data;
  1791. int len = 0;
  1792. len += sprintf(page+len, "%s", MEGARAID_VERSION);
  1793. if(adapter->product_info.product_name[0])
  1794. len += sprintf(page+len, "%s\n",
  1795. adapter->product_info.product_name);
  1796. len += sprintf(page+len, "Controller Type: ");
  1797. if( adapter->flag & BOARD_MEMMAP ) {
  1798. len += sprintf(page+len,
  1799. "438/466/467/471/493/518/520/531/532\n");
  1800. }
  1801. else {
  1802. len += sprintf(page+len,
  1803. "418/428/434\n");
  1804. }
  1805. if(adapter->flag & BOARD_40LD) {
  1806. len += sprintf(page+len,
  1807. "Controller Supports 40 Logical Drives\n");
  1808. }
  1809. if(adapter->flag & BOARD_64BIT) {
  1810. len += sprintf(page+len,
  1811. "Controller capable of 64-bit memory addressing\n");
  1812. }
  1813. if( adapter->has_64bit_addr ) {
  1814. len += sprintf(page+len,
  1815. "Controller using 64-bit memory addressing\n");
  1816. }
  1817. else {
  1818. len += sprintf(page+len,
  1819. "Controller is not using 64-bit memory addressing\n");
  1820. }
  1821. len += sprintf(page+len, "Base = %08lx, Irq = %d, ", adapter->base,
  1822. adapter->host->irq);
  1823. len += sprintf(page+len, "Logical Drives = %d, Channels = %d\n",
  1824. adapter->numldrv, adapter->product_info.nchannels);
  1825. len += sprintf(page+len, "Version =%s:%s, DRAM = %dMb\n",
  1826. adapter->fw_version, adapter->bios_version,
  1827. adapter->product_info.dram_size);
  1828. len += sprintf(page+len,
  1829. "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
  1830. adapter->product_info.max_commands, adapter->max_cmds);
  1831. len += sprintf(page+len, "support_ext_cdb = %d\n",
  1832. adapter->support_ext_cdb);
  1833. len += sprintf(page+len, "support_random_del = %d\n",
  1834. adapter->support_random_del);
  1835. len += sprintf(page+len, "boot_ldrv_enabled = %d\n",
  1836. adapter->boot_ldrv_enabled);
  1837. len += sprintf(page+len, "boot_ldrv = %d\n",
  1838. adapter->boot_ldrv);
  1839. len += sprintf(page+len, "boot_pdrv_enabled = %d\n",
  1840. adapter->boot_pdrv_enabled);
  1841. len += sprintf(page+len, "boot_pdrv_ch = %d\n",
  1842. adapter->boot_pdrv_ch);
  1843. len += sprintf(page+len, "boot_pdrv_tgt = %d\n",
  1844. adapter->boot_pdrv_tgt);
  1845. len += sprintf(page+len, "quiescent = %d\n",
  1846. atomic_read(&adapter->quiescent));
  1847. len += sprintf(page+len, "has_cluster = %d\n",
  1848. adapter->has_cluster);
  1849. len += sprintf(page+len, "\nModule Parameters:\n");
  1850. len += sprintf(page+len, "max_cmd_per_lun = %d\n",
  1851. max_cmd_per_lun);
  1852. len += sprintf(page+len, "max_sectors_per_io = %d\n",
  1853. max_sectors_per_io);
  1854. *eof = 1;
  1855. return len;
  1856. }
  1857. /**
  1858. * proc_read_stat()
  1859. * @page - buffer to write the data in
  1860. * @start - where the actual data has been written in page
  1861. * @offset - same meaning as the read system call
  1862. * @count - same meaning as the read system call
  1863. * @eof - set if no more data needs to be returned
  1864. * @data - pointer to our soft state
  1865. *
  1866. * Diaplay statistical information about the I/O activity.
  1867. */
  1868. static int
  1869. proc_read_stat(char *page, char **start, off_t offset, int count, int *eof,
  1870. void *data)
  1871. {
  1872. adapter_t *adapter;
  1873. int len;
  1874. int i;
  1875. i = 0; /* avoid compilation warnings */
  1876. len = 0;
  1877. adapter = (adapter_t *)data;
  1878. len = sprintf(page, "Statistical Information for this controller\n");
  1879. len += sprintf(page+len, "pend_cmds = %d\n",
  1880. atomic_read(&adapter->pend_cmds));
  1881. #if MEGA_HAVE_STATS
  1882. for(i = 0; i < adapter->numldrv; i++) {
  1883. len += sprintf(page+len, "Logical Drive %d:\n", i);
  1884. len += sprintf(page+len,
  1885. "\tReads Issued = %lu, Writes Issued = %lu\n",
  1886. adapter->nreads[i], adapter->nwrites[i]);
  1887. len += sprintf(page+len,
  1888. "\tSectors Read = %lu, Sectors Written = %lu\n",
  1889. adapter->nreadblocks[i], adapter->nwriteblocks[i]);
  1890. len += sprintf(page+len,
  1891. "\tRead errors = %lu, Write errors = %lu\n\n",
  1892. adapter->rd_errors[i], adapter->wr_errors[i]);
  1893. }
  1894. #else
  1895. len += sprintf(page+len,
  1896. "IO and error counters not compiled in driver.\n");
  1897. #endif
  1898. *eof = 1;
  1899. return len;
  1900. }
  1901. /**
  1902. * proc_read_mbox()
  1903. * @page - buffer to write the data in
  1904. * @start - where the actual data has been written in page
  1905. * @offset - same meaning as the read system call
  1906. * @count - same meaning as the read system call
  1907. * @eof - set if no more data needs to be returned
  1908. * @data - pointer to our soft state
  1909. *
  1910. * Display mailbox information for the last command issued. This information
  1911. * is good for debugging.
  1912. */
  1913. static int
  1914. proc_read_mbox(char *page, char **start, off_t offset, int count, int *eof,
  1915. void *data)
  1916. {
  1917. adapter_t *adapter = (adapter_t *)data;
  1918. volatile mbox_t *mbox = adapter->mbox;
  1919. int len = 0;
  1920. len = sprintf(page, "Contents of Mail Box Structure\n");
  1921. len += sprintf(page+len, " Fw Command = 0x%02x\n",
  1922. mbox->m_out.cmd);
  1923. len += sprintf(page+len, " Cmd Sequence = 0x%02x\n",
  1924. mbox->m_out.cmdid);
  1925. len += sprintf(page+len, " No of Sectors= %04d\n",
  1926. mbox->m_out.numsectors);
  1927. len += sprintf(page+len, " LBA = 0x%02x\n",
  1928. mbox->m_out.lba);
  1929. len += sprintf(page+len, " DTA = 0x%08x\n",
  1930. mbox->m_out.xferaddr);
  1931. len += sprintf(page+len, " Logical Drive= 0x%02x\n",
  1932. mbox->m_out.logdrv);
  1933. len += sprintf(page+len, " No of SG Elmt= 0x%02x\n",
  1934. mbox->m_out.numsgelements);
  1935. len += sprintf(page+len, " Busy = %01x\n",
  1936. mbox->m_in.busy);
  1937. len += sprintf(page+len, " Status = 0x%02x\n",
  1938. mbox->m_in.status);
  1939. *eof = 1;
  1940. return len;
  1941. }
  1942. /**
  1943. * proc_rebuild_rate()
  1944. * @page - buffer to write the data in
  1945. * @start - where the actual data has been written in page
  1946. * @offset - same meaning as the read system call
  1947. * @count - same meaning as the read system call
  1948. * @eof - set if no more data needs to be returned
  1949. * @data - pointer to our soft state
  1950. *
  1951. * Display current rebuild rate
  1952. */
  1953. static int
  1954. proc_rebuild_rate(char *page, char **start, off_t offset, int count, int *eof,
  1955. void *data)
  1956. {
  1957. adapter_t *adapter = (adapter_t *)data;
  1958. dma_addr_t dma_handle;
  1959. caddr_t inquiry;
  1960. struct pci_dev *pdev;
  1961. int len = 0;
  1962. if( make_local_pdev(adapter, &pdev) != 0 ) {
  1963. *eof = 1;
  1964. return len;
  1965. }
  1966. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  1967. free_local_pdev(pdev);
  1968. *eof = 1;
  1969. return len;
  1970. }
  1971. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  1972. len = sprintf(page, "Adapter inquiry failed.\n");
  1973. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  1974. mega_free_inquiry(inquiry, dma_handle, pdev);
  1975. free_local_pdev(pdev);
  1976. *eof = 1;
  1977. return len;
  1978. }
  1979. if( adapter->flag & BOARD_40LD ) {
  1980. len = sprintf(page, "Rebuild Rate: [%d%%]\n",
  1981. ((mega_inquiry3 *)inquiry)->rebuild_rate);
  1982. }
  1983. else {
  1984. len = sprintf(page, "Rebuild Rate: [%d%%]\n",
  1985. ((mraid_ext_inquiry *)
  1986. inquiry)->raid_inq.adapter_info.rebuild_rate);
  1987. }
  1988. mega_free_inquiry(inquiry, dma_handle, pdev);
  1989. free_local_pdev(pdev);
  1990. *eof = 1;
  1991. return len;
  1992. }
  1993. /**
  1994. * proc_battery()
  1995. * @page - buffer to write the data in
  1996. * @start - where the actual data has been written in page
  1997. * @offset - same meaning as the read system call
  1998. * @count - same meaning as the read system call
  1999. * @eof - set if no more data needs to be returned
  2000. * @data - pointer to our soft state
  2001. *
  2002. * Display information about the battery module on the controller.
  2003. */
  2004. static int
  2005. proc_battery(char *page, char **start, off_t offset, int count, int *eof,
  2006. void *data)
  2007. {
  2008. adapter_t *adapter = (adapter_t *)data;
  2009. dma_addr_t dma_handle;
  2010. caddr_t inquiry;
  2011. struct pci_dev *pdev;
  2012. u8 battery_status = 0;
  2013. char str[256];
  2014. int len = 0;
  2015. if( make_local_pdev(adapter, &pdev) != 0 ) {
  2016. *eof = 1;
  2017. return len;
  2018. }
  2019. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  2020. free_local_pdev(pdev);
  2021. *eof = 1;
  2022. return len;
  2023. }
  2024. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  2025. len = sprintf(page, "Adapter inquiry failed.\n");
  2026. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  2027. mega_free_inquiry(inquiry, dma_handle, pdev);
  2028. free_local_pdev(pdev);
  2029. *eof = 1;
  2030. return len;
  2031. }
  2032. if( adapter->flag & BOARD_40LD ) {
  2033. battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
  2034. }
  2035. else {
  2036. battery_status = ((mraid_ext_inquiry *)inquiry)->
  2037. raid_inq.adapter_info.battery_status;
  2038. }
  2039. /*
  2040. * Decode the battery status
  2041. */
  2042. sprintf(str, "Battery Status:[%d]", battery_status);
  2043. if(battery_status == MEGA_BATT_CHARGE_DONE)
  2044. strcat(str, " Charge Done");
  2045. if(battery_status & MEGA_BATT_MODULE_MISSING)
  2046. strcat(str, " Module Missing");
  2047. if(battery_status & MEGA_BATT_LOW_VOLTAGE)
  2048. strcat(str, " Low Voltage");
  2049. if(battery_status & MEGA_BATT_TEMP_HIGH)
  2050. strcat(str, " Temperature High");
  2051. if(battery_status & MEGA_BATT_PACK_MISSING)
  2052. strcat(str, " Pack Missing");
  2053. if(battery_status & MEGA_BATT_CHARGE_INPROG)
  2054. strcat(str, " Charge In-progress");
  2055. if(battery_status & MEGA_BATT_CHARGE_FAIL)
  2056. strcat(str, " Charge Fail");
  2057. if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
  2058. strcat(str, " Cycles Exceeded");
  2059. len = sprintf(page, "%s\n", str);
  2060. mega_free_inquiry(inquiry, dma_handle, pdev);
  2061. free_local_pdev(pdev);
  2062. *eof = 1;
  2063. return len;
  2064. }
  2065. /**
  2066. * proc_pdrv_ch0()
  2067. * @page - buffer to write the data in
  2068. * @start - where the actual data has been written in page
  2069. * @offset - same meaning as the read system call
  2070. * @count - same meaning as the read system call
  2071. * @eof - set if no more data needs to be returned
  2072. * @data - pointer to our soft state
  2073. *
  2074. * Display information about the physical drives on physical channel 0.
  2075. */
  2076. static int
  2077. proc_pdrv_ch0(char *page, char **start, off_t offset, int count, int *eof,
  2078. void *data)
  2079. {
  2080. adapter_t *adapter = (adapter_t *)data;
  2081. *eof = 1;
  2082. return (proc_pdrv(adapter, page, 0));
  2083. }
  2084. /**
  2085. * proc_pdrv_ch1()
  2086. * @page - buffer to write the data in
  2087. * @start - where the actual data has been written in page
  2088. * @offset - same meaning as the read system call
  2089. * @count - same meaning as the read system call
  2090. * @eof - set if no more data needs to be returned
  2091. * @data - pointer to our soft state
  2092. *
  2093. * Display information about the physical drives on physical channel 1.
  2094. */
  2095. static int
  2096. proc_pdrv_ch1(char *page, char **start, off_t offset, int count, int *eof,
  2097. void *data)
  2098. {
  2099. adapter_t *adapter = (adapter_t *)data;
  2100. *eof = 1;
  2101. return (proc_pdrv(adapter, page, 1));
  2102. }
  2103. /**
  2104. * proc_pdrv_ch2()
  2105. * @page - buffer to write the data in
  2106. * @start - where the actual data has been written in page
  2107. * @offset - same meaning as the read system call
  2108. * @count - same meaning as the read system call
  2109. * @eof - set if no more data needs to be returned
  2110. * @data - pointer to our soft state
  2111. *
  2112. * Display information about the physical drives on physical channel 2.
  2113. */
  2114. static int
  2115. proc_pdrv_ch2(char *page, char **start, off_t offset, int count, int *eof,
  2116. void *data)
  2117. {
  2118. adapter_t *adapter = (adapter_t *)data;
  2119. *eof = 1;
  2120. return (proc_pdrv(adapter, page, 2));
  2121. }
  2122. /**
  2123. * proc_pdrv_ch3()
  2124. * @page - buffer to write the data in
  2125. * @start - where the actual data has been written in page
  2126. * @offset - same meaning as the read system call
  2127. * @count - same meaning as the read system call
  2128. * @eof - set if no more data needs to be returned
  2129. * @data - pointer to our soft state
  2130. *
  2131. * Display information about the physical drives on physical channel 3.
  2132. */
  2133. static int
  2134. proc_pdrv_ch3(char *page, char **start, off_t offset, int count, int *eof,
  2135. void *data)
  2136. {
  2137. adapter_t *adapter = (adapter_t *)data;
  2138. *eof = 1;
  2139. return (proc_pdrv(adapter, page, 3));
  2140. }
  2141. /**
  2142. * proc_pdrv()
  2143. * @page - buffer to write the data in
  2144. * @adapter - pointer to our soft state
  2145. *
  2146. * Display information about the physical drives.
  2147. */
  2148. static int
  2149. proc_pdrv(adapter_t *adapter, char *page, int channel)
  2150. {
  2151. dma_addr_t dma_handle;
  2152. char *scsi_inq;
  2153. dma_addr_t scsi_inq_dma_handle;
  2154. caddr_t inquiry;
  2155. struct pci_dev *pdev;
  2156. u8 *pdrv_state;
  2157. u8 state;
  2158. int tgt;
  2159. int max_channels;
  2160. int len = 0;
  2161. char str[80];
  2162. int i;
  2163. if( make_local_pdev(adapter, &pdev) != 0 ) {
  2164. return len;
  2165. }
  2166. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  2167. goto free_pdev;
  2168. }
  2169. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  2170. len = sprintf(page, "Adapter inquiry failed.\n");
  2171. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  2172. goto free_inquiry;
  2173. }
  2174. scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
  2175. if( scsi_inq == NULL ) {
  2176. len = sprintf(page, "memory not available for scsi inq.\n");
  2177. goto free_inquiry;
  2178. }
  2179. if( adapter->flag & BOARD_40LD ) {
  2180. pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
  2181. }
  2182. else {
  2183. pdrv_state = ((mraid_ext_inquiry *)inquiry)->
  2184. raid_inq.pdrv_info.pdrv_state;
  2185. }
  2186. max_channels = adapter->product_info.nchannels;
  2187. if( channel >= max_channels ) {
  2188. goto free_pci;
  2189. }
  2190. for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
  2191. i = channel*16 + tgt;
  2192. state = *(pdrv_state + i);
  2193. switch( state & 0x0F ) {
  2194. case PDRV_ONLINE:
  2195. sprintf(str,
  2196. "Channel:%2d Id:%2d State: Online",
  2197. channel, tgt);
  2198. break;
  2199. case PDRV_FAILED:
  2200. sprintf(str,
  2201. "Channel:%2d Id:%2d State: Failed",
  2202. channel, tgt);
  2203. break;
  2204. case PDRV_RBLD:
  2205. sprintf(str,
  2206. "Channel:%2d Id:%2d State: Rebuild",
  2207. channel, tgt);
  2208. break;
  2209. case PDRV_HOTSPARE:
  2210. sprintf(str,
  2211. "Channel:%2d Id:%2d State: Hot spare",
  2212. channel, tgt);
  2213. break;
  2214. default:
  2215. sprintf(str,
  2216. "Channel:%2d Id:%2d State: Un-configured",
  2217. channel, tgt);
  2218. break;
  2219. }
  2220. /*
  2221. * This interface displays inquiries for disk drives
  2222. * only. Inquries for logical drives and non-disk
  2223. * devices are available through /proc/scsi/scsi
  2224. */
  2225. memset(scsi_inq, 0, 256);
  2226. if( mega_internal_dev_inquiry(adapter, channel, tgt,
  2227. scsi_inq_dma_handle) ||
  2228. (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
  2229. continue;
  2230. }
  2231. /*
  2232. * Check for overflow. We print less than 240
  2233. * characters for inquiry
  2234. */
  2235. if( (len + 240) >= PAGE_SIZE ) break;
  2236. len += sprintf(page+len, "%s.\n", str);
  2237. len += mega_print_inquiry(page+len, scsi_inq);
  2238. }
  2239. free_pci:
  2240. pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
  2241. free_inquiry:
  2242. mega_free_inquiry(inquiry, dma_handle, pdev);
  2243. free_pdev:
  2244. free_local_pdev(pdev);
  2245. return len;
  2246. }
  2247. /*
  2248. * Display scsi inquiry
  2249. */
  2250. static int
  2251. mega_print_inquiry(char *page, char *scsi_inq)
  2252. {
  2253. int len = 0;
  2254. int i;
  2255. len = sprintf(page, " Vendor: ");
  2256. for( i = 8; i < 16; i++ ) {
  2257. len += sprintf(page+len, "%c", scsi_inq[i]);
  2258. }
  2259. len += sprintf(page+len, " Model: ");
  2260. for( i = 16; i < 32; i++ ) {
  2261. len += sprintf(page+len, "%c", scsi_inq[i]);
  2262. }
  2263. len += sprintf(page+len, " Rev: ");
  2264. for( i = 32; i < 36; i++ ) {
  2265. len += sprintf(page+len, "%c", scsi_inq[i]);
  2266. }
  2267. len += sprintf(page+len, "\n");
  2268. i = scsi_inq[0] & 0x1f;
  2269. len += sprintf(page+len, " Type: %s ",
  2270. i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] :
  2271. "Unknown ");
  2272. len += sprintf(page+len,
  2273. " ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
  2274. if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
  2275. len += sprintf(page+len, " CCS\n");
  2276. else
  2277. len += sprintf(page+len, "\n");
  2278. return len;
  2279. }
  2280. /**
  2281. * proc_rdrv_10()
  2282. * @page - buffer to write the data in
  2283. * @start - where the actual data has been written in page
  2284. * @offset - same meaning as the read system call
  2285. * @count - same meaning as the read system call
  2286. * @eof - set if no more data needs to be returned
  2287. * @data - pointer to our soft state
  2288. *
  2289. * Display real time information about the logical drives 0 through 9.
  2290. */
  2291. static int
  2292. proc_rdrv_10(char *page, char **start, off_t offset, int count, int *eof,
  2293. void *data)
  2294. {
  2295. adapter_t *adapter = (adapter_t *)data;
  2296. *eof = 1;
  2297. return (proc_rdrv(adapter, page, 0, 9));
  2298. }
  2299. /**
  2300. * proc_rdrv_20()
  2301. * @page - buffer to write the data in
  2302. * @start - where the actual data has been written in page
  2303. * @offset - same meaning as the read system call
  2304. * @count - same meaning as the read system call
  2305. * @eof - set if no more data needs to be returned
  2306. * @data - pointer to our soft state
  2307. *
  2308. * Display real time information about the logical drives 0 through 9.
  2309. */
  2310. static int
  2311. proc_rdrv_20(char *page, char **start, off_t offset, int count, int *eof,
  2312. void *data)
  2313. {
  2314. adapter_t *adapter = (adapter_t *)data;
  2315. *eof = 1;
  2316. return (proc_rdrv(adapter, page, 10, 19));
  2317. }
  2318. /**
  2319. * proc_rdrv_30()
  2320. * @page - buffer to write the data in
  2321. * @start - where the actual data has been written in page
  2322. * @offset - same meaning as the read system call
  2323. * @count - same meaning as the read system call
  2324. * @eof - set if no more data needs to be returned
  2325. * @data - pointer to our soft state
  2326. *
  2327. * Display real time information about the logical drives 0 through 9.
  2328. */
  2329. static int
  2330. proc_rdrv_30(char *page, char **start, off_t offset, int count, int *eof,
  2331. void *data)
  2332. {
  2333. adapter_t *adapter = (adapter_t *)data;
  2334. *eof = 1;
  2335. return (proc_rdrv(adapter, page, 20, 29));
  2336. }
  2337. /**
  2338. * proc_rdrv_40()
  2339. * @page - buffer to write the data in
  2340. * @start - where the actual data has been written in page
  2341. * @offset - same meaning as the read system call
  2342. * @count - same meaning as the read system call
  2343. * @eof - set if no more data needs to be returned
  2344. * @data - pointer to our soft state
  2345. *
  2346. * Display real time information about the logical drives 0 through 9.
  2347. */
  2348. static int
  2349. proc_rdrv_40(char *page, char **start, off_t offset, int count, int *eof,
  2350. void *data)
  2351. {
  2352. adapter_t *adapter = (adapter_t *)data;
  2353. *eof = 1;
  2354. return (proc_rdrv(adapter, page, 30, 39));
  2355. }
  2356. /**
  2357. * proc_rdrv()
  2358. * @page - buffer to write the data in
  2359. * @adapter - pointer to our soft state
  2360. * @start - starting logical drive to display
  2361. * @end - ending logical drive to display
  2362. *
  2363. * We do not print the inquiry information since its already available through
  2364. * /proc/scsi/scsi interface
  2365. */
  2366. static int
  2367. proc_rdrv(adapter_t *adapter, char *page, int start, int end )
  2368. {
  2369. dma_addr_t dma_handle;
  2370. logdrv_param *lparam;
  2371. megacmd_t mc;
  2372. char *disk_array;
  2373. dma_addr_t disk_array_dma_handle;
  2374. caddr_t inquiry;
  2375. struct pci_dev *pdev;
  2376. u8 *rdrv_state;
  2377. int num_ldrv;
  2378. u32 array_sz;
  2379. int len = 0;
  2380. int i;
  2381. if( make_local_pdev(adapter, &pdev) != 0 ) {
  2382. return len;
  2383. }
  2384. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  2385. free_local_pdev(pdev);
  2386. return len;
  2387. }
  2388. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  2389. len = sprintf(page, "Adapter inquiry failed.\n");
  2390. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  2391. mega_free_inquiry(inquiry, dma_handle, pdev);
  2392. free_local_pdev(pdev);
  2393. return len;
  2394. }
  2395. memset(&mc, 0, sizeof(megacmd_t));
  2396. if( adapter->flag & BOARD_40LD ) {
  2397. array_sz = sizeof(disk_array_40ld);
  2398. rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
  2399. num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
  2400. }
  2401. else {
  2402. array_sz = sizeof(disk_array_8ld);
  2403. rdrv_state = ((mraid_ext_inquiry *)inquiry)->
  2404. raid_inq.logdrv_info.ldrv_state;
  2405. num_ldrv = ((mraid_ext_inquiry *)inquiry)->
  2406. raid_inq.logdrv_info.num_ldrv;
  2407. }
  2408. disk_array = pci_alloc_consistent(pdev, array_sz,
  2409. &disk_array_dma_handle);
  2410. if( disk_array == NULL ) {
  2411. len = sprintf(page, "memory not available.\n");
  2412. mega_free_inquiry(inquiry, dma_handle, pdev);
  2413. free_local_pdev(pdev);
  2414. return len;
  2415. }
  2416. mc.xferaddr = (u32)disk_array_dma_handle;
  2417. if( adapter->flag & BOARD_40LD ) {
  2418. mc.cmd = FC_NEW_CONFIG;
  2419. mc.opcode = OP_DCMD_READ_CONFIG;
  2420. if( mega_internal_command(adapter, &mc, NULL) ) {
  2421. len = sprintf(page, "40LD read config failed.\n");
  2422. mega_free_inquiry(inquiry, dma_handle, pdev);
  2423. pci_free_consistent(pdev, array_sz, disk_array,
  2424. disk_array_dma_handle);
  2425. free_local_pdev(pdev);
  2426. return len;
  2427. }
  2428. }
  2429. else {
  2430. mc.cmd = NEW_READ_CONFIG_8LD;
  2431. if( mega_internal_command(adapter, &mc, NULL) ) {
  2432. mc.cmd = READ_CONFIG_8LD;
  2433. if( mega_internal_command(adapter, &mc,
  2434. NULL) ){
  2435. len = sprintf(page,
  2436. "8LD read config failed.\n");
  2437. mega_free_inquiry(inquiry, dma_handle, pdev);
  2438. pci_free_consistent(pdev, array_sz,
  2439. disk_array,
  2440. disk_array_dma_handle);
  2441. free_local_pdev(pdev);
  2442. return len;
  2443. }
  2444. }
  2445. }
  2446. for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
  2447. if( adapter->flag & BOARD_40LD ) {
  2448. lparam =
  2449. &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
  2450. }
  2451. else {
  2452. lparam =
  2453. &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
  2454. }
  2455. /*
  2456. * Check for overflow. We print less than 240 characters for
  2457. * information about each logical drive.
  2458. */
  2459. if( (len + 240) >= PAGE_SIZE ) break;
  2460. len += sprintf(page+len, "Logical drive:%2d:, ", i);
  2461. switch( rdrv_state[i] & 0x0F ) {
  2462. case RDRV_OFFLINE:
  2463. len += sprintf(page+len, "state: offline");
  2464. break;
  2465. case RDRV_DEGRADED:
  2466. len += sprintf(page+len, "state: degraded");
  2467. break;
  2468. case RDRV_OPTIMAL:
  2469. len += sprintf(page+len, "state: optimal");
  2470. break;
  2471. case RDRV_DELETED:
  2472. len += sprintf(page+len, "state: deleted");
  2473. break;
  2474. default:
  2475. len += sprintf(page+len, "state: unknown");
  2476. break;
  2477. }
  2478. /*
  2479. * Check if check consistency or initialization is going on
  2480. * for this logical drive.
  2481. */
  2482. if( (rdrv_state[i] & 0xF0) == 0x20 ) {
  2483. len += sprintf(page+len,
  2484. ", check-consistency in progress");
  2485. }
  2486. else if( (rdrv_state[i] & 0xF0) == 0x10 ) {
  2487. len += sprintf(page+len,
  2488. ", initialization in progress");
  2489. }
  2490. len += sprintf(page+len, "\n");
  2491. len += sprintf(page+len, "Span depth:%3d, ",
  2492. lparam->span_depth);
  2493. len += sprintf(page+len, "RAID level:%3d, ",
  2494. lparam->level);
  2495. len += sprintf(page+len, "Stripe size:%3d, ",
  2496. lparam->stripe_sz ? lparam->stripe_sz/2: 128);
  2497. len += sprintf(page+len, "Row size:%3d\n",
  2498. lparam->row_size);
  2499. len += sprintf(page+len, "Read Policy: ");
  2500. switch(lparam->read_ahead) {
  2501. case NO_READ_AHEAD:
  2502. len += sprintf(page+len, "No read ahead, ");
  2503. break;
  2504. case READ_AHEAD:
  2505. len += sprintf(page+len, "Read ahead, ");
  2506. break;
  2507. case ADAP_READ_AHEAD:
  2508. len += sprintf(page+len, "Adaptive, ");
  2509. break;
  2510. }
  2511. len += sprintf(page+len, "Write Policy: ");
  2512. switch(lparam->write_mode) {
  2513. case WRMODE_WRITE_THRU:
  2514. len += sprintf(page+len, "Write thru, ");
  2515. break;
  2516. case WRMODE_WRITE_BACK:
  2517. len += sprintf(page+len, "Write back, ");
  2518. break;
  2519. }
  2520. len += sprintf(page+len, "Cache Policy: ");
  2521. switch(lparam->direct_io) {
  2522. case CACHED_IO:
  2523. len += sprintf(page+len, "Cached IO\n\n");
  2524. break;
  2525. case DIRECT_IO:
  2526. len += sprintf(page+len, "Direct IO\n\n");
  2527. break;
  2528. }
  2529. }
  2530. mega_free_inquiry(inquiry, dma_handle, pdev);
  2531. pci_free_consistent(pdev, array_sz, disk_array,
  2532. disk_array_dma_handle);
  2533. free_local_pdev(pdev);
  2534. return len;
  2535. }
  2536. #endif
  2537. /**
  2538. * megaraid_biosparam()
  2539. *
  2540. * Return the disk geometry for a particular disk
  2541. */
  2542. static int
  2543. megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
  2544. sector_t capacity, int geom[])
  2545. {
  2546. adapter_t *adapter;
  2547. unsigned char *bh;
  2548. int heads;
  2549. int sectors;
  2550. int cylinders;
  2551. int rval;
  2552. /* Get pointer to host config structure */
  2553. adapter = (adapter_t *)sdev->host->hostdata;
  2554. if (IS_RAID_CH(adapter, sdev->channel)) {
  2555. /* Default heads (64) & sectors (32) */
  2556. heads = 64;
  2557. sectors = 32;
  2558. cylinders = (ulong)capacity / (heads * sectors);
  2559. /*
  2560. * Handle extended translation size for logical drives
  2561. * > 1Gb
  2562. */
  2563. if ((ulong)capacity >= 0x200000) {
  2564. heads = 255;
  2565. sectors = 63;
  2566. cylinders = (ulong)capacity / (heads * sectors);
  2567. }
  2568. /* return result */
  2569. geom[0] = heads;
  2570. geom[1] = sectors;
  2571. geom[2] = cylinders;
  2572. }
  2573. else {
  2574. bh = scsi_bios_ptable(bdev);
  2575. if( bh ) {
  2576. rval = scsi_partsize(bh, capacity,
  2577. &geom[2], &geom[0], &geom[1]);
  2578. kfree(bh);
  2579. if( rval != -1 )
  2580. return rval;
  2581. }
  2582. printk(KERN_INFO
  2583. "megaraid: invalid partition on this disk on channel %d\n",
  2584. sdev->channel);
  2585. /* Default heads (64) & sectors (32) */
  2586. heads = 64;
  2587. sectors = 32;
  2588. cylinders = (ulong)capacity / (heads * sectors);
  2589. /* Handle extended translation size for logical drives > 1Gb */
  2590. if ((ulong)capacity >= 0x200000) {
  2591. heads = 255;
  2592. sectors = 63;
  2593. cylinders = (ulong)capacity / (heads * sectors);
  2594. }
  2595. /* return result */
  2596. geom[0] = heads;
  2597. geom[1] = sectors;
  2598. geom[2] = cylinders;
  2599. }
  2600. return 0;
  2601. }
  2602. /**
  2603. * mega_init_scb()
  2604. * @adapter - pointer to our soft state
  2605. *
  2606. * Allocate memory for the various pointers in the scb structures:
  2607. * scatter-gather list pointer, passthru and extended passthru structure
  2608. * pointers.
  2609. */
  2610. static int
  2611. mega_init_scb(adapter_t *adapter)
  2612. {
  2613. scb_t *scb;
  2614. int i;
  2615. for( i = 0; i < adapter->max_cmds; i++ ) {
  2616. scb = &adapter->scb_list[i];
  2617. scb->sgl64 = NULL;
  2618. scb->sgl = NULL;
  2619. scb->pthru = NULL;
  2620. scb->epthru = NULL;
  2621. }
  2622. for( i = 0; i < adapter->max_cmds; i++ ) {
  2623. scb = &adapter->scb_list[i];
  2624. scb->idx = i;
  2625. scb->sgl64 = pci_alloc_consistent(adapter->dev,
  2626. sizeof(mega_sgl64) * adapter->sglen,
  2627. &scb->sgl_dma_addr);
  2628. scb->sgl = (mega_sglist *)scb->sgl64;
  2629. if( !scb->sgl ) {
  2630. printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
  2631. mega_free_sgl(adapter);
  2632. return -1;
  2633. }
  2634. scb->pthru = pci_alloc_consistent(adapter->dev,
  2635. sizeof(mega_passthru),
  2636. &scb->pthru_dma_addr);
  2637. if( !scb->pthru ) {
  2638. printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
  2639. mega_free_sgl(adapter);
  2640. return -1;
  2641. }
  2642. scb->epthru = pci_alloc_consistent(adapter->dev,
  2643. sizeof(mega_ext_passthru),
  2644. &scb->epthru_dma_addr);
  2645. if( !scb->epthru ) {
  2646. printk(KERN_WARNING
  2647. "Can't allocate extended passthru.\n");
  2648. mega_free_sgl(adapter);
  2649. return -1;
  2650. }
  2651. scb->dma_type = MEGA_DMA_TYPE_NONE;
  2652. /*
  2653. * Link to free list
  2654. * lock not required since we are loading the driver, so no
  2655. * commands possible right now.
  2656. */
  2657. scb->state = SCB_FREE;
  2658. scb->cmd = NULL;
  2659. list_add(&scb->list, &adapter->free_list);
  2660. }
  2661. return 0;
  2662. }
  2663. /**
  2664. * megadev_open()
  2665. * @inode - unused
  2666. * @filep - unused
  2667. *
  2668. * Routines for the character/ioctl interface to the driver. Find out if this
  2669. * is a valid open. If yes, increment the module use count so that it cannot
  2670. * be unloaded.
  2671. */
  2672. static int
  2673. megadev_open (struct inode *inode, struct file *filep)
  2674. {
  2675. /*
  2676. * Only allow superuser to access private ioctl interface
  2677. */
  2678. if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
  2679. return 0;
  2680. }
  2681. /**
  2682. * megadev_ioctl()
  2683. * @inode - Our device inode
  2684. * @filep - unused
  2685. * @cmd - ioctl command
  2686. * @arg - user buffer
  2687. *
  2688. * ioctl entry point for our private ioctl interface. We move the data in from
  2689. * the user space, prepare the command (if necessary, convert the old MIMD
  2690. * ioctl to new ioctl command), and issue a synchronous command to the
  2691. * controller.
  2692. */
  2693. static int
  2694. megadev_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
  2695. unsigned long arg)
  2696. {
  2697. adapter_t *adapter;
  2698. nitioctl_t uioc;
  2699. int adapno;
  2700. int rval;
  2701. mega_passthru __user *upthru; /* user address for passthru */
  2702. mega_passthru *pthru; /* copy user passthru here */
  2703. dma_addr_t pthru_dma_hndl;
  2704. void *data = NULL; /* data to be transferred */
  2705. dma_addr_t data_dma_hndl; /* dma handle for data xfer area */
  2706. megacmd_t mc;
  2707. megastat_t __user *ustats;
  2708. int num_ldrv;
  2709. u32 uxferaddr = 0;
  2710. struct pci_dev *pdev;
  2711. ustats = NULL; /* avoid compilation warnings */
  2712. num_ldrv = 0;
  2713. /*
  2714. * Make sure only USCSICMD are issued through this interface.
  2715. * MIMD application would still fire different command.
  2716. */
  2717. if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
  2718. return -EINVAL;
  2719. }
  2720. /*
  2721. * Check and convert a possible MIMD command to NIT command.
  2722. * mega_m_to_n() copies the data from the user space, so we do not
  2723. * have to do it here.
  2724. * NOTE: We will need some user address to copyout the data, therefore
  2725. * the inteface layer will also provide us with the required user
  2726. * addresses.
  2727. */
  2728. memset(&uioc, 0, sizeof(nitioctl_t));
  2729. if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
  2730. return rval;
  2731. switch( uioc.opcode ) {
  2732. case GET_DRIVER_VER:
  2733. if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
  2734. return (-EFAULT);
  2735. break;
  2736. case GET_N_ADAP:
  2737. if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
  2738. return (-EFAULT);
  2739. /*
  2740. * Shucks. MIMD interface returns a positive value for number
  2741. * of adapters. TODO: Change it to return 0 when there is no
  2742. * applicatio using mimd interface.
  2743. */
  2744. return hba_count;
  2745. case GET_ADAP_INFO:
  2746. /*
  2747. * Which adapter
  2748. */
  2749. if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  2750. return (-ENODEV);
  2751. if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
  2752. sizeof(struct mcontroller)) )
  2753. return (-EFAULT);
  2754. break;
  2755. #if MEGA_HAVE_STATS
  2756. case GET_STATS:
  2757. /*
  2758. * Which adapter
  2759. */
  2760. if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  2761. return (-ENODEV);
  2762. adapter = hba_soft_state[adapno];
  2763. ustats = uioc.uioc_uaddr;
  2764. if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
  2765. return (-EFAULT);
  2766. /*
  2767. * Check for the validity of the logical drive number
  2768. */
  2769. if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
  2770. if( copy_to_user(ustats->nreads, adapter->nreads,
  2771. num_ldrv*sizeof(u32)) )
  2772. return -EFAULT;
  2773. if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
  2774. num_ldrv*sizeof(u32)) )
  2775. return -EFAULT;
  2776. if( copy_to_user(ustats->nwrites, adapter->nwrites,
  2777. num_ldrv*sizeof(u32)) )
  2778. return -EFAULT;
  2779. if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
  2780. num_ldrv*sizeof(u32)) )
  2781. return -EFAULT;
  2782. if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
  2783. num_ldrv*sizeof(u32)) )
  2784. return -EFAULT;
  2785. if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
  2786. num_ldrv*sizeof(u32)) )
  2787. return -EFAULT;
  2788. return 0;
  2789. #endif
  2790. case MBOX_CMD:
  2791. /*
  2792. * Which adapter
  2793. */
  2794. if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  2795. return (-ENODEV);
  2796. adapter = hba_soft_state[adapno];
  2797. /*
  2798. * Deletion of logical drive is a special case. The adapter
  2799. * should be quiescent before this command is issued.
  2800. */
  2801. if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
  2802. uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
  2803. /*
  2804. * Do we support this feature
  2805. */
  2806. if( !adapter->support_random_del ) {
  2807. printk(KERN_WARNING "megaraid: logdrv ");
  2808. printk("delete on non-supporting F/W.\n");
  2809. return (-EINVAL);
  2810. }
  2811. rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
  2812. if( rval == 0 ) {
  2813. memset(&mc, 0, sizeof(megacmd_t));
  2814. mc.status = rval;
  2815. rval = mega_n_to_m((void __user *)arg, &mc);
  2816. }
  2817. return rval;
  2818. }
  2819. /*
  2820. * This interface only support the regular passthru commands.
  2821. * Reject extended passthru and 64-bit passthru
  2822. */
  2823. if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
  2824. uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
  2825. printk(KERN_WARNING "megaraid: rejected passthru.\n");
  2826. return (-EINVAL);
  2827. }
  2828. /*
  2829. * For all internal commands, the buffer must be allocated in
  2830. * <4GB address range
  2831. */
  2832. if( make_local_pdev(adapter, &pdev) != 0 )
  2833. return -EIO;
  2834. /* Is it a passthru command or a DCMD */
  2835. if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
  2836. /* Passthru commands */
  2837. pthru = pci_alloc_consistent(pdev,
  2838. sizeof(mega_passthru),
  2839. &pthru_dma_hndl);
  2840. if( pthru == NULL ) {
  2841. free_local_pdev(pdev);
  2842. return (-ENOMEM);
  2843. }
  2844. /*
  2845. * The user passthru structure
  2846. */
  2847. upthru = (mega_passthru __user *)MBOX(uioc)->xferaddr;
  2848. /*
  2849. * Copy in the user passthru here.
  2850. */
  2851. if( copy_from_user(pthru, upthru,
  2852. sizeof(mega_passthru)) ) {
  2853. pci_free_consistent(pdev,
  2854. sizeof(mega_passthru), pthru,
  2855. pthru_dma_hndl);
  2856. free_local_pdev(pdev);
  2857. return (-EFAULT);
  2858. }
  2859. /*
  2860. * Is there a data transfer
  2861. */
  2862. if( pthru->dataxferlen ) {
  2863. data = pci_alloc_consistent(pdev,
  2864. pthru->dataxferlen,
  2865. &data_dma_hndl);
  2866. if( data == NULL ) {
  2867. pci_free_consistent(pdev,
  2868. sizeof(mega_passthru),
  2869. pthru,
  2870. pthru_dma_hndl);
  2871. free_local_pdev(pdev);
  2872. return (-ENOMEM);
  2873. }
  2874. /*
  2875. * Save the user address and point the kernel
  2876. * address at just allocated memory
  2877. */
  2878. uxferaddr = pthru->dataxferaddr;
  2879. pthru->dataxferaddr = data_dma_hndl;
  2880. }
  2881. /*
  2882. * Is data coming down-stream
  2883. */
  2884. if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
  2885. /*
  2886. * Get the user data
  2887. */
  2888. if( copy_from_user(data, (char __user *)uxferaddr,
  2889. pthru->dataxferlen) ) {
  2890. rval = (-EFAULT);
  2891. goto freemem_and_return;
  2892. }
  2893. }
  2894. memset(&mc, 0, sizeof(megacmd_t));
  2895. mc.cmd = MEGA_MBOXCMD_PASSTHRU;
  2896. mc.xferaddr = (u32)pthru_dma_hndl;
  2897. /*
  2898. * Issue the command
  2899. */
  2900. mega_internal_command(adapter, &mc, pthru);
  2901. rval = mega_n_to_m((void __user *)arg, &mc);
  2902. if( rval ) goto freemem_and_return;
  2903. /*
  2904. * Is data going up-stream
  2905. */
  2906. if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
  2907. if( copy_to_user((char __user *)uxferaddr, data,
  2908. pthru->dataxferlen) ) {
  2909. rval = (-EFAULT);
  2910. }
  2911. }
  2912. /*
  2913. * Send the request sense data also, irrespective of
  2914. * whether the user has asked for it or not.
  2915. */
  2916. copy_to_user(upthru->reqsensearea,
  2917. pthru->reqsensearea, 14);
  2918. freemem_and_return:
  2919. if( pthru->dataxferlen ) {
  2920. pci_free_consistent(pdev,
  2921. pthru->dataxferlen, data,
  2922. data_dma_hndl);
  2923. }
  2924. pci_free_consistent(pdev, sizeof(mega_passthru),
  2925. pthru, pthru_dma_hndl);
  2926. free_local_pdev(pdev);
  2927. return rval;
  2928. }
  2929. else {
  2930. /* DCMD commands */
  2931. /*
  2932. * Is there a data transfer
  2933. */
  2934. if( uioc.xferlen ) {
  2935. data = pci_alloc_consistent(pdev,
  2936. uioc.xferlen, &data_dma_hndl);
  2937. if( data == NULL ) {
  2938. free_local_pdev(pdev);
  2939. return (-ENOMEM);
  2940. }
  2941. uxferaddr = MBOX(uioc)->xferaddr;
  2942. }
  2943. /*
  2944. * Is data coming down-stream
  2945. */
  2946. if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
  2947. /*
  2948. * Get the user data
  2949. */
  2950. if( copy_from_user(data, (char __user *)uxferaddr,
  2951. uioc.xferlen) ) {
  2952. pci_free_consistent(pdev,
  2953. uioc.xferlen,
  2954. data, data_dma_hndl);
  2955. free_local_pdev(pdev);
  2956. return (-EFAULT);
  2957. }
  2958. }
  2959. memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
  2960. mc.xferaddr = (u32)data_dma_hndl;
  2961. /*
  2962. * Issue the command
  2963. */
  2964. mega_internal_command(adapter, &mc, NULL);
  2965. rval = mega_n_to_m((void __user *)arg, &mc);
  2966. if( rval ) {
  2967. if( uioc.xferlen ) {
  2968. pci_free_consistent(pdev,
  2969. uioc.xferlen, data,
  2970. data_dma_hndl);
  2971. }
  2972. free_local_pdev(pdev);
  2973. return rval;
  2974. }
  2975. /*
  2976. * Is data going up-stream
  2977. */
  2978. if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
  2979. if( copy_to_user((char __user *)uxferaddr, data,
  2980. uioc.xferlen) ) {
  2981. rval = (-EFAULT);
  2982. }
  2983. }
  2984. if( uioc.xferlen ) {
  2985. pci_free_consistent(pdev,
  2986. uioc.xferlen, data,
  2987. data_dma_hndl);
  2988. }
  2989. free_local_pdev(pdev);
  2990. return rval;
  2991. }
  2992. default:
  2993. return (-EINVAL);
  2994. }
  2995. return 0;
  2996. }
  2997. /**
  2998. * mega_m_to_n()
  2999. * @arg - user address
  3000. * @uioc - new ioctl structure
  3001. *
  3002. * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
  3003. * structure
  3004. *
  3005. * Converts the older mimd ioctl structure to newer NIT structure
  3006. */
  3007. static int
  3008. mega_m_to_n(void __user *arg, nitioctl_t *uioc)
  3009. {
  3010. struct uioctl_t uioc_mimd;
  3011. char signature[8] = {0};
  3012. u8 opcode;
  3013. u8 subopcode;
  3014. /*
  3015. * check is the application conforms to NIT. We do not have to do much
  3016. * in that case.
  3017. * We exploit the fact that the signature is stored in the very
  3018. * begining of the structure.
  3019. */
  3020. if( copy_from_user(signature, arg, 7) )
  3021. return (-EFAULT);
  3022. if( memcmp(signature, "MEGANIT", 7) == 0 ) {
  3023. /*
  3024. * NOTE NOTE: The nit ioctl is still under flux because of
  3025. * change of mailbox definition, in HPE. No applications yet
  3026. * use this interface and let's not have applications use this
  3027. * interface till the new specifitions are in place.
  3028. */
  3029. return -EINVAL;
  3030. #if 0
  3031. if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
  3032. return (-EFAULT);
  3033. return 0;
  3034. #endif
  3035. }
  3036. /*
  3037. * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
  3038. *
  3039. * Get the user ioctl structure
  3040. */
  3041. if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
  3042. return (-EFAULT);
  3043. /*
  3044. * Get the opcode and subopcode for the commands
  3045. */
  3046. opcode = uioc_mimd.ui.fcs.opcode;
  3047. subopcode = uioc_mimd.ui.fcs.subopcode;
  3048. switch (opcode) {
  3049. case 0x82:
  3050. switch (subopcode) {
  3051. case MEGAIOC_QDRVRVER: /* Query driver version */
  3052. uioc->opcode = GET_DRIVER_VER;
  3053. uioc->uioc_uaddr = uioc_mimd.data;
  3054. break;
  3055. case MEGAIOC_QNADAP: /* Get # of adapters */
  3056. uioc->opcode = GET_N_ADAP;
  3057. uioc->uioc_uaddr = uioc_mimd.data;
  3058. break;
  3059. case MEGAIOC_QADAPINFO: /* Get adapter information */
  3060. uioc->opcode = GET_ADAP_INFO;
  3061. uioc->adapno = uioc_mimd.ui.fcs.adapno;
  3062. uioc->uioc_uaddr = uioc_mimd.data;
  3063. break;
  3064. default:
  3065. return(-EINVAL);
  3066. }
  3067. break;
  3068. case 0x81:
  3069. uioc->opcode = MBOX_CMD;
  3070. uioc->adapno = uioc_mimd.ui.fcs.adapno;
  3071. memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
  3072. uioc->xferlen = uioc_mimd.ui.fcs.length;
  3073. if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
  3074. if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
  3075. break;
  3076. case 0x80:
  3077. uioc->opcode = MBOX_CMD;
  3078. uioc->adapno = uioc_mimd.ui.fcs.adapno;
  3079. memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
  3080. /*
  3081. * Choose the xferlen bigger of input and output data
  3082. */
  3083. uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
  3084. uioc_mimd.outlen : uioc_mimd.inlen;
  3085. if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
  3086. if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
  3087. break;
  3088. default:
  3089. return (-EINVAL);
  3090. }
  3091. return 0;
  3092. }
  3093. /*
  3094. * mega_n_to_m()
  3095. * @arg - user address
  3096. * @mc - mailbox command
  3097. *
  3098. * Updates the status information to the application, depending on application
  3099. * conforms to older mimd ioctl interface or newer NIT ioctl interface
  3100. */
  3101. static int
  3102. mega_n_to_m(void __user *arg, megacmd_t *mc)
  3103. {
  3104. nitioctl_t __user *uiocp;
  3105. megacmd_t __user *umc;
  3106. mega_passthru __user *upthru;
  3107. struct uioctl_t __user *uioc_mimd;
  3108. char signature[8] = {0};
  3109. /*
  3110. * check is the application conforms to NIT.
  3111. */
  3112. if( copy_from_user(signature, arg, 7) )
  3113. return -EFAULT;
  3114. if( memcmp(signature, "MEGANIT", 7) == 0 ) {
  3115. uiocp = arg;
  3116. if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
  3117. return (-EFAULT);
  3118. if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
  3119. umc = MBOX_P(uiocp);
  3120. if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
  3121. return -EFAULT;
  3122. if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
  3123. return (-EFAULT);
  3124. }
  3125. }
  3126. else {
  3127. uioc_mimd = arg;
  3128. if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
  3129. return (-EFAULT);
  3130. if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
  3131. umc = (megacmd_t __user *)uioc_mimd->mbox;
  3132. if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
  3133. return (-EFAULT);
  3134. if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
  3135. return (-EFAULT);
  3136. }
  3137. }
  3138. return 0;
  3139. }
  3140. /*
  3141. * MEGARAID 'FW' commands.
  3142. */
  3143. /**
  3144. * mega_is_bios_enabled()
  3145. * @adapter - pointer to our soft state
  3146. *
  3147. * issue command to find out if the BIOS is enabled for this controller
  3148. */
  3149. static int
  3150. mega_is_bios_enabled(adapter_t *adapter)
  3151. {
  3152. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3153. mbox_t *mbox;
  3154. int ret;
  3155. mbox = (mbox_t *)raw_mbox;
  3156. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3157. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3158. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3159. raw_mbox[0] = IS_BIOS_ENABLED;
  3160. raw_mbox[2] = GET_BIOS;
  3161. ret = issue_scb_block(adapter, raw_mbox);
  3162. return *(char *)adapter->mega_buffer;
  3163. }
  3164. /**
  3165. * mega_enum_raid_scsi()
  3166. * @adapter - pointer to our soft state
  3167. *
  3168. * Find out what channels are RAID/SCSI. This information is used to
  3169. * differentiate the virtual channels and physical channels and to support
  3170. * ROMB feature and non-disk devices.
  3171. */
  3172. static void
  3173. mega_enum_raid_scsi(adapter_t *adapter)
  3174. {
  3175. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3176. mbox_t *mbox;
  3177. int i;
  3178. mbox = (mbox_t *)raw_mbox;
  3179. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3180. /*
  3181. * issue command to find out what channels are raid/scsi
  3182. */
  3183. raw_mbox[0] = CHNL_CLASS;
  3184. raw_mbox[2] = GET_CHNL_CLASS;
  3185. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3186. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3187. /*
  3188. * Non-ROMB firmware fail this command, so all channels
  3189. * must be shown RAID
  3190. */
  3191. adapter->mega_ch_class = 0xFF;
  3192. if(!issue_scb_block(adapter, raw_mbox)) {
  3193. adapter->mega_ch_class = *((char *)adapter->mega_buffer);
  3194. }
  3195. for( i = 0; i < adapter->product_info.nchannels; i++ ) {
  3196. if( (adapter->mega_ch_class >> i) & 0x01 ) {
  3197. printk(KERN_INFO "megaraid: channel[%d] is raid.\n",
  3198. i);
  3199. }
  3200. else {
  3201. printk(KERN_INFO "megaraid: channel[%d] is scsi.\n",
  3202. i);
  3203. }
  3204. }
  3205. return;
  3206. }
  3207. /**
  3208. * mega_get_boot_drv()
  3209. * @adapter - pointer to our soft state
  3210. *
  3211. * Find out which device is the boot device. Note, any logical drive or any
  3212. * phyical device (e.g., a CDROM) can be designated as a boot device.
  3213. */
  3214. static void
  3215. mega_get_boot_drv(adapter_t *adapter)
  3216. {
  3217. struct private_bios_data *prv_bios_data;
  3218. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3219. mbox_t *mbox;
  3220. u16 cksum = 0;
  3221. u8 *cksum_p;
  3222. u8 boot_pdrv;
  3223. int i;
  3224. mbox = (mbox_t *)raw_mbox;
  3225. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3226. raw_mbox[0] = BIOS_PVT_DATA;
  3227. raw_mbox[2] = GET_BIOS_PVT_DATA;
  3228. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3229. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3230. adapter->boot_ldrv_enabled = 0;
  3231. adapter->boot_ldrv = 0;
  3232. adapter->boot_pdrv_enabled = 0;
  3233. adapter->boot_pdrv_ch = 0;
  3234. adapter->boot_pdrv_tgt = 0;
  3235. if(issue_scb_block(adapter, raw_mbox) == 0) {
  3236. prv_bios_data =
  3237. (struct private_bios_data *)adapter->mega_buffer;
  3238. cksum = 0;
  3239. cksum_p = (char *)prv_bios_data;
  3240. for (i = 0; i < 14; i++ ) {
  3241. cksum += (u16)(*cksum_p++);
  3242. }
  3243. if (prv_bios_data->cksum == (u16)(0-cksum) ) {
  3244. /*
  3245. * If MSB is set, a physical drive is set as boot
  3246. * device
  3247. */
  3248. if( prv_bios_data->boot_drv & 0x80 ) {
  3249. adapter->boot_pdrv_enabled = 1;
  3250. boot_pdrv = prv_bios_data->boot_drv & 0x7F;
  3251. adapter->boot_pdrv_ch = boot_pdrv / 16;
  3252. adapter->boot_pdrv_tgt = boot_pdrv % 16;
  3253. }
  3254. else {
  3255. adapter->boot_ldrv_enabled = 1;
  3256. adapter->boot_ldrv = prv_bios_data->boot_drv;
  3257. }
  3258. }
  3259. }
  3260. }
  3261. /**
  3262. * mega_support_random_del()
  3263. * @adapter - pointer to our soft state
  3264. *
  3265. * Find out if this controller supports random deletion and addition of
  3266. * logical drives
  3267. */
  3268. static int
  3269. mega_support_random_del(adapter_t *adapter)
  3270. {
  3271. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3272. mbox_t *mbox;
  3273. int rval;
  3274. mbox = (mbox_t *)raw_mbox;
  3275. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3276. /*
  3277. * issue command
  3278. */
  3279. raw_mbox[0] = FC_DEL_LOGDRV;
  3280. raw_mbox[2] = OP_SUP_DEL_LOGDRV;
  3281. rval = issue_scb_block(adapter, raw_mbox);
  3282. return !rval;
  3283. }
  3284. /**
  3285. * mega_support_ext_cdb()
  3286. * @adapter - pointer to our soft state
  3287. *
  3288. * Find out if this firmware support cdblen > 10
  3289. */
  3290. static int
  3291. mega_support_ext_cdb(adapter_t *adapter)
  3292. {
  3293. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3294. mbox_t *mbox;
  3295. int rval;
  3296. mbox = (mbox_t *)raw_mbox;
  3297. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3298. /*
  3299. * issue command to find out if controller supports extended CDBs.
  3300. */
  3301. raw_mbox[0] = 0xA4;
  3302. raw_mbox[2] = 0x16;
  3303. rval = issue_scb_block(adapter, raw_mbox);
  3304. return !rval;
  3305. }
  3306. /**
  3307. * mega_del_logdrv()
  3308. * @adapter - pointer to our soft state
  3309. * @logdrv - logical drive to be deleted
  3310. *
  3311. * Delete the specified logical drive. It is the responsibility of the user
  3312. * app to let the OS know about this operation.
  3313. */
  3314. static int
  3315. mega_del_logdrv(adapter_t *adapter, int logdrv)
  3316. {
  3317. unsigned long flags;
  3318. scb_t *scb;
  3319. int rval;
  3320. /*
  3321. * Stop sending commands to the controller, queue them internally.
  3322. * When deletion is complete, ISR will flush the queue.
  3323. */
  3324. atomic_set(&adapter->quiescent, 1);
  3325. /*
  3326. * Wait till all the issued commands are complete and there are no
  3327. * commands in the pending queue
  3328. */
  3329. while (atomic_read(&adapter->pend_cmds) > 0 ||
  3330. !list_empty(&adapter->pending_list))
  3331. msleep(1000); /* sleep for 1s */
  3332. rval = mega_do_del_logdrv(adapter, logdrv);
  3333. spin_lock_irqsave(&adapter->lock, flags);
  3334. /*
  3335. * If delete operation was successful, add 0x80 to the logical drive
  3336. * ids for commands in the pending queue.
  3337. */
  3338. if (adapter->read_ldidmap) {
  3339. struct list_head *pos;
  3340. list_for_each(pos, &adapter->pending_list) {
  3341. scb = list_entry(pos, scb_t, list);
  3342. if (scb->pthru->logdrv < 0x80 )
  3343. scb->pthru->logdrv += 0x80;
  3344. }
  3345. }
  3346. atomic_set(&adapter->quiescent, 0);
  3347. mega_runpendq(adapter);
  3348. spin_unlock_irqrestore(&adapter->lock, flags);
  3349. return rval;
  3350. }
  3351. static int
  3352. mega_do_del_logdrv(adapter_t *adapter, int logdrv)
  3353. {
  3354. megacmd_t mc;
  3355. int rval;
  3356. memset( &mc, 0, sizeof(megacmd_t));
  3357. mc.cmd = FC_DEL_LOGDRV;
  3358. mc.opcode = OP_DEL_LOGDRV;
  3359. mc.subopcode = logdrv;
  3360. rval = mega_internal_command(adapter, &mc, NULL);
  3361. /* log this event */
  3362. if(rval) {
  3363. printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
  3364. return rval;
  3365. }
  3366. /*
  3367. * After deleting first logical drive, the logical drives must be
  3368. * addressed by adding 0x80 to the logical drive id.
  3369. */
  3370. adapter->read_ldidmap = 1;
  3371. return rval;
  3372. }
  3373. /**
  3374. * mega_get_max_sgl()
  3375. * @adapter - pointer to our soft state
  3376. *
  3377. * Find out the maximum number of scatter-gather elements supported by this
  3378. * version of the firmware
  3379. */
  3380. static void
  3381. mega_get_max_sgl(adapter_t *adapter)
  3382. {
  3383. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3384. mbox_t *mbox;
  3385. mbox = (mbox_t *)raw_mbox;
  3386. memset(mbox, 0, sizeof(raw_mbox));
  3387. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3388. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3389. raw_mbox[0] = MAIN_MISC_OPCODE;
  3390. raw_mbox[2] = GET_MAX_SG_SUPPORT;
  3391. if( issue_scb_block(adapter, raw_mbox) ) {
  3392. /*
  3393. * f/w does not support this command. Choose the default value
  3394. */
  3395. adapter->sglen = MIN_SGLIST;
  3396. }
  3397. else {
  3398. adapter->sglen = *((char *)adapter->mega_buffer);
  3399. /*
  3400. * Make sure this is not more than the resources we are
  3401. * planning to allocate
  3402. */
  3403. if ( adapter->sglen > MAX_SGLIST )
  3404. adapter->sglen = MAX_SGLIST;
  3405. }
  3406. return;
  3407. }
  3408. /**
  3409. * mega_support_cluster()
  3410. * @adapter - pointer to our soft state
  3411. *
  3412. * Find out if this firmware support cluster calls.
  3413. */
  3414. static int
  3415. mega_support_cluster(adapter_t *adapter)
  3416. {
  3417. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3418. mbox_t *mbox;
  3419. mbox = (mbox_t *)raw_mbox;
  3420. memset(mbox, 0, sizeof(raw_mbox));
  3421. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3422. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3423. /*
  3424. * Try to get the initiator id. This command will succeed iff the
  3425. * clustering is available on this HBA.
  3426. */
  3427. raw_mbox[0] = MEGA_GET_TARGET_ID;
  3428. if( issue_scb_block(adapter, raw_mbox) == 0 ) {
  3429. /*
  3430. * Cluster support available. Get the initiator target id.
  3431. * Tell our id to mid-layer too.
  3432. */
  3433. adapter->this_id = *(u32 *)adapter->mega_buffer;
  3434. adapter->host->this_id = adapter->this_id;
  3435. return 1;
  3436. }
  3437. return 0;
  3438. }
  3439. /**
  3440. * mega_adapinq()
  3441. * @adapter - pointer to our soft state
  3442. * @dma_handle - DMA address of the buffer
  3443. *
  3444. * Issue internal comamnds while interrupts are available.
  3445. * We only issue direct mailbox commands from within the driver. ioctl()
  3446. * interface using these routines can issue passthru commands.
  3447. */
  3448. static int
  3449. mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
  3450. {
  3451. megacmd_t mc;
  3452. memset(&mc, 0, sizeof(megacmd_t));
  3453. if( adapter->flag & BOARD_40LD ) {
  3454. mc.cmd = FC_NEW_CONFIG;
  3455. mc.opcode = NC_SUBOP_ENQUIRY3;
  3456. mc.subopcode = ENQ3_GET_SOLICITED_FULL;
  3457. }
  3458. else {
  3459. mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
  3460. }
  3461. mc.xferaddr = (u32)dma_handle;
  3462. if ( mega_internal_command(adapter, &mc, NULL) != 0 ) {
  3463. return -1;
  3464. }
  3465. return 0;
  3466. }
  3467. /** mega_internal_dev_inquiry()
  3468. * @adapter - pointer to our soft state
  3469. * @ch - channel for this device
  3470. * @tgt - ID of this device
  3471. * @buf_dma_handle - DMA address of the buffer
  3472. *
  3473. * Issue the scsi inquiry for the specified device.
  3474. */
  3475. static int
  3476. mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
  3477. dma_addr_t buf_dma_handle)
  3478. {
  3479. mega_passthru *pthru;
  3480. dma_addr_t pthru_dma_handle;
  3481. megacmd_t mc;
  3482. int rval;
  3483. struct pci_dev *pdev;
  3484. /*
  3485. * For all internal commands, the buffer must be allocated in <4GB
  3486. * address range
  3487. */
  3488. if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
  3489. pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
  3490. &pthru_dma_handle);
  3491. if( pthru == NULL ) {
  3492. free_local_pdev(pdev);
  3493. return -1;
  3494. }
  3495. pthru->timeout = 2;
  3496. pthru->ars = 1;
  3497. pthru->reqsenselen = 14;
  3498. pthru->islogical = 0;
  3499. pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
  3500. pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
  3501. pthru->cdblen = 6;
  3502. pthru->cdb[0] = INQUIRY;
  3503. pthru->cdb[1] = 0;
  3504. pthru->cdb[2] = 0;
  3505. pthru->cdb[3] = 0;
  3506. pthru->cdb[4] = 255;
  3507. pthru->cdb[5] = 0;
  3508. pthru->dataxferaddr = (u32)buf_dma_handle;
  3509. pthru->dataxferlen = 256;
  3510. memset(&mc, 0, sizeof(megacmd_t));
  3511. mc.cmd = MEGA_MBOXCMD_PASSTHRU;
  3512. mc.xferaddr = (u32)pthru_dma_handle;
  3513. rval = mega_internal_command(adapter, &mc, pthru);
  3514. pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
  3515. pthru_dma_handle);
  3516. free_local_pdev(pdev);
  3517. return rval;
  3518. }
  3519. /**
  3520. * mega_internal_command()
  3521. * @adapter - pointer to our soft state
  3522. * @mc - the mailbox command
  3523. * @pthru - Passthru structure for DCDB commands
  3524. *
  3525. * Issue the internal commands in interrupt mode.
  3526. * The last argument is the address of the passthru structure if the command
  3527. * to be fired is a passthru command
  3528. *
  3529. * lockscope specifies whether the caller has already acquired the lock. Of
  3530. * course, the caller must know which lock we are talking about.
  3531. *
  3532. * Note: parameter 'pthru' is null for non-passthru commands.
  3533. */
  3534. static int
  3535. mega_internal_command(adapter_t *adapter, megacmd_t *mc, mega_passthru *pthru)
  3536. {
  3537. Scsi_Cmnd *scmd;
  3538. struct scsi_device *sdev;
  3539. unsigned long flags = 0;
  3540. scb_t *scb;
  3541. int rval;
  3542. /*
  3543. * The internal commands share one command id and hence are
  3544. * serialized. This is so because we want to reserve maximum number of
  3545. * available command ids for the I/O commands.
  3546. */
  3547. mutex_lock(&adapter->int_mtx);
  3548. scb = &adapter->int_scb;
  3549. memset(scb, 0, sizeof(scb_t));
  3550. scmd = &adapter->int_scmd;
  3551. memset(scmd, 0, sizeof(Scsi_Cmnd));
  3552. sdev = kmalloc(sizeof(struct scsi_device), GFP_KERNEL);
  3553. memset(sdev, 0, sizeof(struct scsi_device));
  3554. scmd->device = sdev;
  3555. scmd->device->host = adapter->host;
  3556. scmd->buffer = (void *)scb;
  3557. scmd->cmnd[0] = MEGA_INTERNAL_CMD;
  3558. scb->state |= SCB_ACTIVE;
  3559. scb->cmd = scmd;
  3560. memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
  3561. /*
  3562. * Is it a passthru command
  3563. */
  3564. if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
  3565. scb->pthru = pthru;
  3566. }
  3567. scb->idx = CMDID_INT_CMDS;
  3568. megaraid_queue(scmd, mega_internal_done);
  3569. wait_for_completion(&adapter->int_waitq);
  3570. rval = scmd->result;
  3571. mc->status = scmd->result;
  3572. kfree(sdev);
  3573. /*
  3574. * Print a debug message for all failed commands. Applications can use
  3575. * this information.
  3576. */
  3577. if( scmd->result && trace_level ) {
  3578. printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
  3579. mc->cmd, mc->opcode, mc->subopcode, scmd->result);
  3580. }
  3581. mutex_unlock(&adapter->int_mtx);
  3582. return rval;
  3583. }
  3584. /**
  3585. * mega_internal_done()
  3586. * @scmd - internal scsi command
  3587. *
  3588. * Callback routine for internal commands.
  3589. */
  3590. static void
  3591. mega_internal_done(Scsi_Cmnd *scmd)
  3592. {
  3593. adapter_t *adapter;
  3594. adapter = (adapter_t *)scmd->device->host->hostdata;
  3595. complete(&adapter->int_waitq);
  3596. }
  3597. static struct scsi_host_template megaraid_template = {
  3598. .module = THIS_MODULE,
  3599. .name = "MegaRAID",
  3600. .proc_name = "megaraid_legacy",
  3601. .info = megaraid_info,
  3602. .queuecommand = megaraid_queue,
  3603. .bios_param = megaraid_biosparam,
  3604. .max_sectors = MAX_SECTORS_PER_IO,
  3605. .can_queue = MAX_COMMANDS,
  3606. .this_id = DEFAULT_INITIATOR_ID,
  3607. .sg_tablesize = MAX_SGLIST,
  3608. .cmd_per_lun = DEF_CMD_PER_LUN,
  3609. .use_clustering = ENABLE_CLUSTERING,
  3610. .eh_abort_handler = megaraid_abort,
  3611. .eh_device_reset_handler = megaraid_reset,
  3612. .eh_bus_reset_handler = megaraid_reset,
  3613. .eh_host_reset_handler = megaraid_reset,
  3614. };
  3615. static int __devinit
  3616. megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  3617. {
  3618. struct Scsi_Host *host;
  3619. adapter_t *adapter;
  3620. unsigned long mega_baseport, tbase, flag = 0;
  3621. u16 subsysid, subsysvid;
  3622. u8 pci_bus, pci_dev_func;
  3623. int irq, i, j;
  3624. int error = -ENODEV;
  3625. if (pci_enable_device(pdev))
  3626. goto out;
  3627. pci_set_master(pdev);
  3628. pci_bus = pdev->bus->number;
  3629. pci_dev_func = pdev->devfn;
  3630. /*
  3631. * The megaraid3 stuff reports the ID of the Intel part which is not
  3632. * remotely specific to the megaraid
  3633. */
  3634. if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  3635. u16 magic;
  3636. /*
  3637. * Don't fall over the Compaq management cards using the same
  3638. * PCI identifier
  3639. */
  3640. if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
  3641. pdev->subsystem_device == 0xC000)
  3642. return -ENODEV;
  3643. /* Now check the magic signature byte */
  3644. pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
  3645. if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
  3646. return -ENODEV;
  3647. /* Ok it is probably a megaraid */
  3648. }
  3649. /*
  3650. * For these vendor and device ids, signature offsets are not
  3651. * valid and 64 bit is implicit
  3652. */
  3653. if (id->driver_data & BOARD_64BIT)
  3654. flag |= BOARD_64BIT;
  3655. else {
  3656. u32 magic64;
  3657. pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
  3658. if (magic64 == HBA_SIGNATURE_64BIT)
  3659. flag |= BOARD_64BIT;
  3660. }
  3661. subsysvid = pdev->subsystem_vendor;
  3662. subsysid = pdev->subsystem_device;
  3663. printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
  3664. id->vendor, id->device, pci_bus);
  3665. printk("slot %d:func %d\n",
  3666. PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
  3667. /* Read the base port and IRQ from PCI */
  3668. mega_baseport = pci_resource_start(pdev, 0);
  3669. irq = pdev->irq;
  3670. tbase = mega_baseport;
  3671. if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
  3672. flag |= BOARD_MEMMAP;
  3673. if (!request_mem_region(mega_baseport, 128, "megaraid")) {
  3674. printk(KERN_WARNING "megaraid: mem region busy!\n");
  3675. goto out_disable_device;
  3676. }
  3677. mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
  3678. if (!mega_baseport) {
  3679. printk(KERN_WARNING
  3680. "megaraid: could not map hba memory\n");
  3681. goto out_release_region;
  3682. }
  3683. } else {
  3684. flag |= BOARD_IOMAP;
  3685. mega_baseport += 0x10;
  3686. if (!request_region(mega_baseport, 16, "megaraid"))
  3687. goto out_disable_device;
  3688. }
  3689. /* Initialize SCSI Host structure */
  3690. host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
  3691. if (!host)
  3692. goto out_iounmap;
  3693. adapter = (adapter_t *)host->hostdata;
  3694. memset(adapter, 0, sizeof(adapter_t));
  3695. printk(KERN_NOTICE
  3696. "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
  3697. host->host_no, mega_baseport, irq);
  3698. adapter->base = mega_baseport;
  3699. INIT_LIST_HEAD(&adapter->free_list);
  3700. INIT_LIST_HEAD(&adapter->pending_list);
  3701. INIT_LIST_HEAD(&adapter->completed_list);
  3702. adapter->flag = flag;
  3703. spin_lock_init(&adapter->lock);
  3704. host->cmd_per_lun = max_cmd_per_lun;
  3705. host->max_sectors = max_sectors_per_io;
  3706. adapter->dev = pdev;
  3707. adapter->host = host;
  3708. adapter->host->irq = irq;
  3709. if (flag & BOARD_MEMMAP)
  3710. adapter->host->base = tbase;
  3711. else {
  3712. adapter->host->io_port = tbase;
  3713. adapter->host->n_io_port = 16;
  3714. }
  3715. adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
  3716. /*
  3717. * Allocate buffer to issue internal commands.
  3718. */
  3719. adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
  3720. MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
  3721. if (!adapter->mega_buffer) {
  3722. printk(KERN_WARNING "megaraid: out of RAM.\n");
  3723. goto out_host_put;
  3724. }
  3725. adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
  3726. if (!adapter->scb_list) {
  3727. printk(KERN_WARNING "megaraid: out of RAM.\n");
  3728. goto out_free_cmd_buffer;
  3729. }
  3730. if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
  3731. megaraid_isr_memmapped : megaraid_isr_iomapped,
  3732. SA_SHIRQ, "megaraid", adapter)) {
  3733. printk(KERN_WARNING
  3734. "megaraid: Couldn't register IRQ %d!\n", irq);
  3735. goto out_free_scb_list;
  3736. }
  3737. if (mega_setup_mailbox(adapter))
  3738. goto out_free_irq;
  3739. if (mega_query_adapter(adapter))
  3740. goto out_free_mbox;
  3741. /*
  3742. * Have checks for some buggy f/w
  3743. */
  3744. if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
  3745. /*
  3746. * Which firmware
  3747. */
  3748. if (!strcmp(adapter->fw_version, "3.00") ||
  3749. !strcmp(adapter->fw_version, "3.01")) {
  3750. printk( KERN_WARNING
  3751. "megaraid: Your card is a Dell PERC "
  3752. "2/SC RAID controller with "
  3753. "firmware\nmegaraid: 3.00 or 3.01. "
  3754. "This driver is known to have "
  3755. "corruption issues\nmegaraid: with "
  3756. "those firmware versions on this "
  3757. "specific card. In order\nmegaraid: "
  3758. "to protect your data, please upgrade "
  3759. "your firmware to version\nmegaraid: "
  3760. "3.10 or later, available from the "
  3761. "Dell Technical Support web\n"
  3762. "megaraid: site at\nhttp://support."
  3763. "dell.com/us/en/filelib/download/"
  3764. "index.asp?fileid=2940\n"
  3765. );
  3766. }
  3767. }
  3768. /*
  3769. * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
  3770. * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
  3771. * support, since this firmware cannot handle 64 bit
  3772. * addressing
  3773. */
  3774. if ((subsysvid == HP_SUBSYS_VID) &&
  3775. ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
  3776. /*
  3777. * which firmware
  3778. */
  3779. if (!strcmp(adapter->fw_version, "H01.07") ||
  3780. !strcmp(adapter->fw_version, "H01.08") ||
  3781. !strcmp(adapter->fw_version, "H01.09") ) {
  3782. printk(KERN_WARNING
  3783. "megaraid: Firmware H.01.07, "
  3784. "H.01.08, and H.01.09 on 1M/2M "
  3785. "controllers\n"
  3786. "megaraid: do not support 64 bit "
  3787. "addressing.\nmegaraid: DISABLING "
  3788. "64 bit support.\n");
  3789. adapter->flag &= ~BOARD_64BIT;
  3790. }
  3791. }
  3792. if (mega_is_bios_enabled(adapter))
  3793. mega_hbas[hba_count].is_bios_enabled = 1;
  3794. mega_hbas[hba_count].hostdata_addr = adapter;
  3795. /*
  3796. * Find out which channel is raid and which is scsi. This is
  3797. * for ROMB support.
  3798. */
  3799. mega_enum_raid_scsi(adapter);
  3800. /*
  3801. * Find out if a logical drive is set as the boot drive. If
  3802. * there is one, will make that as the first logical drive.
  3803. * ROMB: Do we have to boot from a physical drive. Then all
  3804. * the physical drives would appear before the logical disks.
  3805. * Else, all the physical drives would be exported to the mid
  3806. * layer after logical drives.
  3807. */
  3808. mega_get_boot_drv(adapter);
  3809. if (adapter->boot_pdrv_enabled) {
  3810. j = adapter->product_info.nchannels;
  3811. for( i = 0; i < j; i++ )
  3812. adapter->logdrv_chan[i] = 0;
  3813. for( i = j; i < NVIRT_CHAN + j; i++ )
  3814. adapter->logdrv_chan[i] = 1;
  3815. } else {
  3816. for (i = 0; i < NVIRT_CHAN; i++)
  3817. adapter->logdrv_chan[i] = 1;
  3818. for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
  3819. adapter->logdrv_chan[i] = 0;
  3820. adapter->mega_ch_class <<= NVIRT_CHAN;
  3821. }
  3822. /*
  3823. * Do we support random deletion and addition of logical
  3824. * drives
  3825. */
  3826. adapter->read_ldidmap = 0; /* set it after first logdrv
  3827. delete cmd */
  3828. adapter->support_random_del = mega_support_random_del(adapter);
  3829. /* Initialize SCBs */
  3830. if (mega_init_scb(adapter))
  3831. goto out_free_mbox;
  3832. /*
  3833. * Reset the pending commands counter
  3834. */
  3835. atomic_set(&adapter->pend_cmds, 0);
  3836. /*
  3837. * Reset the adapter quiescent flag
  3838. */
  3839. atomic_set(&adapter->quiescent, 0);
  3840. hba_soft_state[hba_count] = adapter;
  3841. /*
  3842. * Fill in the structure which needs to be passed back to the
  3843. * application when it does an ioctl() for controller related
  3844. * information.
  3845. */
  3846. i = hba_count;
  3847. mcontroller[i].base = mega_baseport;
  3848. mcontroller[i].irq = irq;
  3849. mcontroller[i].numldrv = adapter->numldrv;
  3850. mcontroller[i].pcibus = pci_bus;
  3851. mcontroller[i].pcidev = id->device;
  3852. mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
  3853. mcontroller[i].pciid = -1;
  3854. mcontroller[i].pcivendor = id->vendor;
  3855. mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
  3856. mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
  3857. /* Set the Mode of addressing to 64 bit if we can */
  3858. if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
  3859. pci_set_dma_mask(pdev, DMA_64BIT_MASK);
  3860. adapter->has_64bit_addr = 1;
  3861. } else {
  3862. pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  3863. adapter->has_64bit_addr = 0;
  3864. }
  3865. mutex_init(&adapter->int_mtx);
  3866. init_completion(&adapter->int_waitq);
  3867. adapter->this_id = DEFAULT_INITIATOR_ID;
  3868. adapter->host->this_id = DEFAULT_INITIATOR_ID;
  3869. #if MEGA_HAVE_CLUSTERING
  3870. /*
  3871. * Is cluster support enabled on this controller
  3872. * Note: In a cluster the HBAs ( the initiators ) will have
  3873. * different target IDs and we cannot assume it to be 7. Call
  3874. * to mega_support_cluster() will get the target ids also if
  3875. * the cluster support is available
  3876. */
  3877. adapter->has_cluster = mega_support_cluster(adapter);
  3878. if (adapter->has_cluster) {
  3879. printk(KERN_NOTICE
  3880. "megaraid: Cluster driver, initiator id:%d\n",
  3881. adapter->this_id);
  3882. }
  3883. #endif
  3884. pci_set_drvdata(pdev, host);
  3885. mega_create_proc_entry(hba_count, mega_proc_dir_entry);
  3886. error = scsi_add_host(host, &pdev->dev);
  3887. if (error)
  3888. goto out_free_mbox;
  3889. scsi_scan_host(host);
  3890. hba_count++;
  3891. return 0;
  3892. out_free_mbox:
  3893. pci_free_consistent(adapter->dev, sizeof(mbox64_t),
  3894. adapter->una_mbox64, adapter->una_mbox64_dma);
  3895. out_free_irq:
  3896. free_irq(adapter->host->irq, adapter);
  3897. out_free_scb_list:
  3898. kfree(adapter->scb_list);
  3899. out_free_cmd_buffer:
  3900. pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
  3901. adapter->mega_buffer, adapter->buf_dma_handle);
  3902. out_host_put:
  3903. scsi_host_put(host);
  3904. out_iounmap:
  3905. if (flag & BOARD_MEMMAP)
  3906. iounmap((void *)mega_baseport);
  3907. out_release_region:
  3908. if (flag & BOARD_MEMMAP)
  3909. release_mem_region(tbase, 128);
  3910. else
  3911. release_region(mega_baseport, 16);
  3912. out_disable_device:
  3913. pci_disable_device(pdev);
  3914. out:
  3915. return error;
  3916. }
  3917. static void
  3918. __megaraid_shutdown(adapter_t *adapter)
  3919. {
  3920. u_char raw_mbox[sizeof(struct mbox_out)];
  3921. mbox_t *mbox = (mbox_t *)raw_mbox;
  3922. int i;
  3923. /* Flush adapter cache */
  3924. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3925. raw_mbox[0] = FLUSH_ADAPTER;
  3926. free_irq(adapter->host->irq, adapter);
  3927. /* Issue a blocking (interrupts disabled) command to the card */
  3928. issue_scb_block(adapter, raw_mbox);
  3929. /* Flush disks cache */
  3930. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3931. raw_mbox[0] = FLUSH_SYSTEM;
  3932. /* Issue a blocking (interrupts disabled) command to the card */
  3933. issue_scb_block(adapter, raw_mbox);
  3934. if (atomic_read(&adapter->pend_cmds) > 0)
  3935. printk(KERN_WARNING "megaraid: pending commands!!\n");
  3936. /*
  3937. * Have a delibrate delay to make sure all the caches are
  3938. * actually flushed.
  3939. */
  3940. for (i = 0; i <= 10; i++)
  3941. mdelay(1000);
  3942. }
  3943. static void
  3944. megaraid_remove_one(struct pci_dev *pdev)
  3945. {
  3946. struct Scsi_Host *host = pci_get_drvdata(pdev);
  3947. adapter_t *adapter = (adapter_t *)host->hostdata;
  3948. char buf[12] = { 0 };
  3949. scsi_remove_host(host);
  3950. __megaraid_shutdown(adapter);
  3951. /* Free our resources */
  3952. if (adapter->flag & BOARD_MEMMAP) {
  3953. iounmap((void *)adapter->base);
  3954. release_mem_region(adapter->host->base, 128);
  3955. } else
  3956. release_region(adapter->base, 16);
  3957. mega_free_sgl(adapter);
  3958. #ifdef CONFIG_PROC_FS
  3959. if (adapter->controller_proc_dir_entry) {
  3960. remove_proc_entry("stat", adapter->controller_proc_dir_entry);
  3961. remove_proc_entry("config",
  3962. adapter->controller_proc_dir_entry);
  3963. remove_proc_entry("mailbox",
  3964. adapter->controller_proc_dir_entry);
  3965. #if MEGA_HAVE_ENH_PROC
  3966. remove_proc_entry("rebuild-rate",
  3967. adapter->controller_proc_dir_entry);
  3968. remove_proc_entry("battery-status",
  3969. adapter->controller_proc_dir_entry);
  3970. remove_proc_entry("diskdrives-ch0",
  3971. adapter->controller_proc_dir_entry);
  3972. remove_proc_entry("diskdrives-ch1",
  3973. adapter->controller_proc_dir_entry);
  3974. remove_proc_entry("diskdrives-ch2",
  3975. adapter->controller_proc_dir_entry);
  3976. remove_proc_entry("diskdrives-ch3",
  3977. adapter->controller_proc_dir_entry);
  3978. remove_proc_entry("raiddrives-0-9",
  3979. adapter->controller_proc_dir_entry);
  3980. remove_proc_entry("raiddrives-10-19",
  3981. adapter->controller_proc_dir_entry);
  3982. remove_proc_entry("raiddrives-20-29",
  3983. adapter->controller_proc_dir_entry);
  3984. remove_proc_entry("raiddrives-30-39",
  3985. adapter->controller_proc_dir_entry);
  3986. #endif
  3987. sprintf(buf, "hba%d", adapter->host->host_no);
  3988. remove_proc_entry(buf, mega_proc_dir_entry);
  3989. }
  3990. #endif
  3991. pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
  3992. adapter->mega_buffer, adapter->buf_dma_handle);
  3993. kfree(adapter->scb_list);
  3994. pci_free_consistent(adapter->dev, sizeof(mbox64_t),
  3995. adapter->una_mbox64, adapter->una_mbox64_dma);
  3996. scsi_host_put(host);
  3997. pci_disable_device(pdev);
  3998. hba_count--;
  3999. }
  4000. static void
  4001. megaraid_shutdown(struct pci_dev *pdev)
  4002. {
  4003. struct Scsi_Host *host = pci_get_drvdata(pdev);
  4004. adapter_t *adapter = (adapter_t *)host->hostdata;
  4005. __megaraid_shutdown(adapter);
  4006. }
  4007. static struct pci_device_id megaraid_pci_tbl[] = {
  4008. {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
  4009. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4010. {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
  4011. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4012. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
  4013. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4014. {0,}
  4015. };
  4016. MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
  4017. static struct pci_driver megaraid_pci_driver = {
  4018. .name = "megaraid_legacy",
  4019. .id_table = megaraid_pci_tbl,
  4020. .probe = megaraid_probe_one,
  4021. .remove = __devexit_p(megaraid_remove_one),
  4022. .shutdown = megaraid_shutdown,
  4023. };
  4024. static int __init megaraid_init(void)
  4025. {
  4026. int error;
  4027. if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
  4028. max_cmd_per_lun = MAX_CMD_PER_LUN;
  4029. if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
  4030. max_mbox_busy_wait = MBOX_BUSY_WAIT;
  4031. #ifdef CONFIG_PROC_FS
  4032. mega_proc_dir_entry = proc_mkdir("megaraid", &proc_root);
  4033. if (!mega_proc_dir_entry) {
  4034. printk(KERN_WARNING
  4035. "megaraid: failed to create megaraid root\n");
  4036. }
  4037. #endif
  4038. error = pci_module_init(&megaraid_pci_driver);
  4039. if (error) {
  4040. #ifdef CONFIG_PROC_FS
  4041. remove_proc_entry("megaraid", &proc_root);
  4042. #endif
  4043. return error;
  4044. }
  4045. /*
  4046. * Register the driver as a character device, for applications
  4047. * to access it for ioctls.
  4048. * First argument (major) to register_chrdev implies a dynamic
  4049. * major number allocation.
  4050. */
  4051. major = register_chrdev(0, "megadev_legacy", &megadev_fops);
  4052. if (!major) {
  4053. printk(KERN_WARNING
  4054. "megaraid: failed to register char device\n");
  4055. }
  4056. return 0;
  4057. }
  4058. static void __exit megaraid_exit(void)
  4059. {
  4060. /*
  4061. * Unregister the character device interface to the driver.
  4062. */
  4063. unregister_chrdev(major, "megadev_legacy");
  4064. pci_unregister_driver(&megaraid_pci_driver);
  4065. #ifdef CONFIG_PROC_FS
  4066. remove_proc_entry("megaraid", &proc_root);
  4067. #endif
  4068. }
  4069. module_init(megaraid_init);
  4070. module_exit(megaraid_exit);
  4071. /* vi: set ts=8 sw=8 tw=78: */