megaraid_mbox.c 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144
  1. /*
  2. *
  3. * Linux MegaRAID device driver
  4. *
  5. * Copyright (c) 2003-2004 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. * FILE : megaraid_mbox.c
  13. * Version : v2.20.5.1 (Nov 16 2006)
  14. *
  15. * Authors:
  16. * Atul Mukker <Atul.Mukker@lsi.com>
  17. * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsi.com>
  18. * Manoj Jose <Manoj.Jose@lsi.com>
  19. * Seokmann Ju
  20. *
  21. * List of supported controllers
  22. *
  23. * OEM Product Name VID DID SSVID SSID
  24. * --- ------------ --- --- ---- ----
  25. * Dell PERC3/QC 101E 1960 1028 0471
  26. * Dell PERC3/DC 101E 1960 1028 0493
  27. * Dell PERC3/SC 101E 1960 1028 0475
  28. * Dell PERC3/Di 1028 1960 1028 0123
  29. * Dell PERC4/SC 1000 1960 1028 0520
  30. * Dell PERC4/DC 1000 1960 1028 0518
  31. * Dell PERC4/QC 1000 0407 1028 0531
  32. * Dell PERC4/Di 1028 000F 1028 014A
  33. * Dell PERC 4e/Si 1028 0013 1028 016c
  34. * Dell PERC 4e/Di 1028 0013 1028 016d
  35. * Dell PERC 4e/Di 1028 0013 1028 016e
  36. * Dell PERC 4e/Di 1028 0013 1028 016f
  37. * Dell PERC 4e/Di 1028 0013 1028 0170
  38. * Dell PERC 4e/DC 1000 0408 1028 0002
  39. * Dell PERC 4e/SC 1000 0408 1028 0001
  40. *
  41. *
  42. * LSI MegaRAID SCSI 320-0 1000 1960 1000 A520
  43. * LSI MegaRAID SCSI 320-1 1000 1960 1000 0520
  44. * LSI MegaRAID SCSI 320-2 1000 1960 1000 0518
  45. * LSI MegaRAID SCSI 320-0X 1000 0407 1000 0530
  46. * LSI MegaRAID SCSI 320-2X 1000 0407 1000 0532
  47. * LSI MegaRAID SCSI 320-4X 1000 0407 1000 0531
  48. * LSI MegaRAID SCSI 320-1E 1000 0408 1000 0001
  49. * LSI MegaRAID SCSI 320-2E 1000 0408 1000 0002
  50. * LSI MegaRAID SATA 150-4 1000 1960 1000 4523
  51. * LSI MegaRAID SATA 150-6 1000 1960 1000 0523
  52. * LSI MegaRAID SATA 300-4X 1000 0409 1000 3004
  53. * LSI MegaRAID SATA 300-8X 1000 0409 1000 3008
  54. *
  55. * INTEL RAID Controller SRCU42X 1000 0407 8086 0532
  56. * INTEL RAID Controller SRCS16 1000 1960 8086 0523
  57. * INTEL RAID Controller SRCU42E 1000 0408 8086 0002
  58. * INTEL RAID Controller SRCZCRX 1000 0407 8086 0530
  59. * INTEL RAID Controller SRCS28X 1000 0409 8086 3008
  60. * INTEL RAID Controller SROMBU42E 1000 0408 8086 3431
  61. * INTEL RAID Controller SROMBU42E 1000 0408 8086 3499
  62. * INTEL RAID Controller SRCU51L 1000 1960 8086 0520
  63. *
  64. * FSC MegaRAID PCI Express ROMB 1000 0408 1734 1065
  65. *
  66. * ACER MegaRAID ROMB-2E 1000 0408 1025 004D
  67. *
  68. * NEC MegaRAID PCI Express ROMB 1000 0408 1033 8287
  69. *
  70. * For history of changes, see Documentation/scsi/ChangeLog.megaraid
  71. */
  72. #include <linux/slab.h>
  73. #include <linux/module.h>
  74. #include "megaraid_mbox.h"
  75. static int megaraid_init(void);
  76. static void megaraid_exit(void);
  77. static int megaraid_probe_one(struct pci_dev*, const struct pci_device_id *);
  78. static void megaraid_detach_one(struct pci_dev *);
  79. static void megaraid_mbox_shutdown(struct pci_dev *);
  80. static int megaraid_io_attach(adapter_t *);
  81. static void megaraid_io_detach(adapter_t *);
  82. static int megaraid_init_mbox(adapter_t *);
  83. static void megaraid_fini_mbox(adapter_t *);
  84. static int megaraid_alloc_cmd_packets(adapter_t *);
  85. static void megaraid_free_cmd_packets(adapter_t *);
  86. static int megaraid_mbox_setup_dma_pools(adapter_t *);
  87. static void megaraid_mbox_teardown_dma_pools(adapter_t *);
  88. static int megaraid_sysfs_alloc_resources(adapter_t *);
  89. static void megaraid_sysfs_free_resources(adapter_t *);
  90. static int megaraid_abort_handler(struct scsi_cmnd *);
  91. static int megaraid_reset_handler(struct scsi_cmnd *);
  92. static int mbox_post_sync_cmd(adapter_t *, uint8_t []);
  93. static int mbox_post_sync_cmd_fast(adapter_t *, uint8_t []);
  94. static int megaraid_busywait_mbox(mraid_device_t *);
  95. static int megaraid_mbox_product_info(adapter_t *);
  96. static int megaraid_mbox_extended_cdb(adapter_t *);
  97. static int megaraid_mbox_support_ha(adapter_t *, uint16_t *);
  98. static int megaraid_mbox_support_random_del(adapter_t *);
  99. static int megaraid_mbox_get_max_sg(adapter_t *);
  100. static void megaraid_mbox_enum_raid_scsi(adapter_t *);
  101. static void megaraid_mbox_flush_cache(adapter_t *);
  102. static int megaraid_mbox_fire_sync_cmd(adapter_t *);
  103. static void megaraid_mbox_display_scb(adapter_t *, scb_t *);
  104. static void megaraid_mbox_setup_device_map(adapter_t *);
  105. static int megaraid_queue_command(struct Scsi_Host *, struct scsi_cmnd *);
  106. static scb_t *megaraid_mbox_build_cmd(adapter_t *, struct scsi_cmnd *, int *);
  107. static void megaraid_mbox_runpendq(adapter_t *, scb_t *);
  108. static void megaraid_mbox_prepare_pthru(adapter_t *, scb_t *,
  109. struct scsi_cmnd *);
  110. static void megaraid_mbox_prepare_epthru(adapter_t *, scb_t *,
  111. struct scsi_cmnd *);
  112. static irqreturn_t megaraid_isr(int, void *);
  113. static void megaraid_mbox_dpc(unsigned long);
  114. static ssize_t megaraid_sysfs_show_app_hndl(struct device *, struct device_attribute *attr, char *);
  115. static ssize_t megaraid_sysfs_show_ldnum(struct device *, struct device_attribute *attr, char *);
  116. static int megaraid_cmm_register(adapter_t *);
  117. static int megaraid_cmm_unregister(adapter_t *);
  118. static int megaraid_mbox_mm_handler(unsigned long, uioc_t *, uint32_t);
  119. static int megaraid_mbox_mm_command(adapter_t *, uioc_t *);
  120. static void megaraid_mbox_mm_done(adapter_t *, scb_t *);
  121. static int gather_hbainfo(adapter_t *, mraid_hba_info_t *);
  122. static int wait_till_fw_empty(adapter_t *);
  123. MODULE_AUTHOR("megaraidlinux@lsi.com");
  124. MODULE_DESCRIPTION("LSI Logic MegaRAID Mailbox Driver");
  125. MODULE_LICENSE("GPL");
  126. MODULE_VERSION(MEGARAID_VERSION);
  127. /*
  128. * ### modules parameters for driver ###
  129. */
  130. /*
  131. * Set to enable driver to expose unconfigured disk to kernel
  132. */
  133. static int megaraid_expose_unconf_disks = 0;
  134. module_param_named(unconf_disks, megaraid_expose_unconf_disks, int, 0);
  135. MODULE_PARM_DESC(unconf_disks,
  136. "Set to expose unconfigured disks to kernel (default=0)");
  137. /*
  138. * driver wait time if the adapter's mailbox is busy
  139. */
  140. static unsigned int max_mbox_busy_wait = MBOX_BUSY_WAIT;
  141. module_param_named(busy_wait, max_mbox_busy_wait, int, 0);
  142. MODULE_PARM_DESC(busy_wait,
  143. "Max wait for mailbox in microseconds if busy (default=10)");
  144. /*
  145. * number of sectors per IO command
  146. */
  147. static unsigned int megaraid_max_sectors = MBOX_MAX_SECTORS;
  148. module_param_named(max_sectors, megaraid_max_sectors, int, 0);
  149. MODULE_PARM_DESC(max_sectors,
  150. "Maximum number of sectors per IO command (default=128)");
  151. /*
  152. * number of commands per logical unit
  153. */
  154. static unsigned int megaraid_cmd_per_lun = MBOX_DEF_CMD_PER_LUN;
  155. module_param_named(cmd_per_lun, megaraid_cmd_per_lun, int, 0);
  156. MODULE_PARM_DESC(cmd_per_lun,
  157. "Maximum number of commands per logical unit (default=64)");
  158. /*
  159. * Fast driver load option, skip scanning for physical devices during load.
  160. * This would result in non-disk devices being skipped during driver load
  161. * time. These can be later added though, using /proc/scsi/scsi
  162. */
  163. static unsigned int megaraid_fast_load = 0;
  164. module_param_named(fast_load, megaraid_fast_load, int, 0);
  165. MODULE_PARM_DESC(fast_load,
  166. "Faster loading of the driver, skips physical devices! (default=0)");
  167. /*
  168. * mraid_debug level - threshold for amount of information to be displayed by
  169. * the driver. This level can be changed through modules parameters, ioctl or
  170. * sysfs/proc interface. By default, print the announcement messages only.
  171. */
  172. int mraid_debug_level = CL_ANN;
  173. module_param_named(debug_level, mraid_debug_level, int, 0);
  174. MODULE_PARM_DESC(debug_level, "Debug level for driver (default=0)");
  175. /*
  176. * ### global data ###
  177. */
  178. static uint8_t megaraid_mbox_version[8] =
  179. { 0x02, 0x20, 0x04, 0x06, 3, 7, 20, 5 };
  180. /*
  181. * PCI table for all supported controllers.
  182. */
  183. static struct pci_device_id pci_id_table_g[] = {
  184. {
  185. PCI_VENDOR_ID_DELL,
  186. PCI_DEVICE_ID_PERC4_DI_DISCOVERY,
  187. PCI_VENDOR_ID_DELL,
  188. PCI_SUBSYS_ID_PERC4_DI_DISCOVERY,
  189. },
  190. {
  191. PCI_VENDOR_ID_LSI_LOGIC,
  192. PCI_DEVICE_ID_PERC4_SC,
  193. PCI_VENDOR_ID_DELL,
  194. PCI_SUBSYS_ID_PERC4_SC,
  195. },
  196. {
  197. PCI_VENDOR_ID_LSI_LOGIC,
  198. PCI_DEVICE_ID_PERC4_DC,
  199. PCI_VENDOR_ID_DELL,
  200. PCI_SUBSYS_ID_PERC4_DC,
  201. },
  202. {
  203. PCI_VENDOR_ID_LSI_LOGIC,
  204. PCI_DEVICE_ID_VERDE,
  205. PCI_ANY_ID,
  206. PCI_ANY_ID,
  207. },
  208. {
  209. PCI_VENDOR_ID_DELL,
  210. PCI_DEVICE_ID_PERC4_DI_EVERGLADES,
  211. PCI_VENDOR_ID_DELL,
  212. PCI_SUBSYS_ID_PERC4_DI_EVERGLADES,
  213. },
  214. {
  215. PCI_VENDOR_ID_DELL,
  216. PCI_DEVICE_ID_PERC4E_SI_BIGBEND,
  217. PCI_VENDOR_ID_DELL,
  218. PCI_SUBSYS_ID_PERC4E_SI_BIGBEND,
  219. },
  220. {
  221. PCI_VENDOR_ID_DELL,
  222. PCI_DEVICE_ID_PERC4E_DI_KOBUK,
  223. PCI_VENDOR_ID_DELL,
  224. PCI_SUBSYS_ID_PERC4E_DI_KOBUK,
  225. },
  226. {
  227. PCI_VENDOR_ID_DELL,
  228. PCI_DEVICE_ID_PERC4E_DI_CORVETTE,
  229. PCI_VENDOR_ID_DELL,
  230. PCI_SUBSYS_ID_PERC4E_DI_CORVETTE,
  231. },
  232. {
  233. PCI_VENDOR_ID_DELL,
  234. PCI_DEVICE_ID_PERC4E_DI_EXPEDITION,
  235. PCI_VENDOR_ID_DELL,
  236. PCI_SUBSYS_ID_PERC4E_DI_EXPEDITION,
  237. },
  238. {
  239. PCI_VENDOR_ID_DELL,
  240. PCI_DEVICE_ID_PERC4E_DI_GUADALUPE,
  241. PCI_VENDOR_ID_DELL,
  242. PCI_SUBSYS_ID_PERC4E_DI_GUADALUPE,
  243. },
  244. {
  245. PCI_VENDOR_ID_LSI_LOGIC,
  246. PCI_DEVICE_ID_DOBSON,
  247. PCI_ANY_ID,
  248. PCI_ANY_ID,
  249. },
  250. {
  251. PCI_VENDOR_ID_AMI,
  252. PCI_DEVICE_ID_AMI_MEGARAID3,
  253. PCI_ANY_ID,
  254. PCI_ANY_ID,
  255. },
  256. {
  257. PCI_VENDOR_ID_LSI_LOGIC,
  258. PCI_DEVICE_ID_AMI_MEGARAID3,
  259. PCI_ANY_ID,
  260. PCI_ANY_ID,
  261. },
  262. {
  263. PCI_VENDOR_ID_LSI_LOGIC,
  264. PCI_DEVICE_ID_LINDSAY,
  265. PCI_ANY_ID,
  266. PCI_ANY_ID,
  267. },
  268. {0} /* Terminating entry */
  269. };
  270. MODULE_DEVICE_TABLE(pci, pci_id_table_g);
  271. static struct pci_driver megaraid_pci_driver = {
  272. .name = "megaraid",
  273. .id_table = pci_id_table_g,
  274. .probe = megaraid_probe_one,
  275. .remove = megaraid_detach_one,
  276. .shutdown = megaraid_mbox_shutdown,
  277. };
  278. // definitions for the device attributes for exporting logical drive number
  279. // for a scsi address (Host, Channel, Id, Lun)
  280. DEVICE_ATTR(megaraid_mbox_app_hndl, S_IRUSR, megaraid_sysfs_show_app_hndl,
  281. NULL);
  282. // Host template initializer for megaraid mbox sysfs device attributes
  283. static struct device_attribute *megaraid_shost_attrs[] = {
  284. &dev_attr_megaraid_mbox_app_hndl,
  285. NULL,
  286. };
  287. DEVICE_ATTR(megaraid_mbox_ld, S_IRUSR, megaraid_sysfs_show_ldnum, NULL);
  288. // Host template initializer for megaraid mbox sysfs device attributes
  289. static struct device_attribute *megaraid_sdev_attrs[] = {
  290. &dev_attr_megaraid_mbox_ld,
  291. NULL,
  292. };
  293. /*
  294. * Scsi host template for megaraid unified driver
  295. */
  296. static struct scsi_host_template megaraid_template_g = {
  297. .module = THIS_MODULE,
  298. .name = "LSI Logic MegaRAID driver",
  299. .proc_name = "megaraid",
  300. .queuecommand = megaraid_queue_command,
  301. .eh_abort_handler = megaraid_abort_handler,
  302. .eh_host_reset_handler = megaraid_reset_handler,
  303. .change_queue_depth = scsi_change_queue_depth,
  304. .use_clustering = ENABLE_CLUSTERING,
  305. .no_write_same = 1,
  306. .sdev_attrs = megaraid_sdev_attrs,
  307. .shost_attrs = megaraid_shost_attrs,
  308. };
  309. /**
  310. * megaraid_init - module load hook
  311. *
  312. * We register ourselves as hotplug enabled module and let PCI subsystem
  313. * discover our adapters.
  314. */
  315. static int __init
  316. megaraid_init(void)
  317. {
  318. int rval;
  319. // Announce the driver version
  320. con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
  321. MEGARAID_EXT_VERSION));
  322. // check validity of module parameters
  323. if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
  324. con_log(CL_ANN, (KERN_WARNING
  325. "megaraid mailbox: max commands per lun reset to %d\n",
  326. MBOX_MAX_SCSI_CMDS));
  327. megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
  328. }
  329. // register as a PCI hot-plug driver module
  330. rval = pci_register_driver(&megaraid_pci_driver);
  331. if (rval < 0) {
  332. con_log(CL_ANN, (KERN_WARNING
  333. "megaraid: could not register hotplug support.\n"));
  334. }
  335. return rval;
  336. }
  337. /**
  338. * megaraid_exit - driver unload entry point
  339. *
  340. * We simply unwrap the megaraid_init routine here.
  341. */
  342. static void __exit
  343. megaraid_exit(void)
  344. {
  345. con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
  346. // unregister as PCI hotplug driver
  347. pci_unregister_driver(&megaraid_pci_driver);
  348. return;
  349. }
  350. /**
  351. * megaraid_probe_one - PCI hotplug entry point
  352. * @pdev : handle to this controller's PCI configuration space
  353. * @id : pci device id of the class of controllers
  354. *
  355. * This routine should be called whenever a new adapter is detected by the
  356. * PCI hotplug susbsystem.
  357. */
  358. static int
  359. megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  360. {
  361. adapter_t *adapter;
  362. // detected a new controller
  363. con_log(CL_ANN, (KERN_INFO
  364. "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
  365. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  366. pdev->subsystem_device));
  367. con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
  368. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
  369. if (pci_enable_device(pdev)) {
  370. con_log(CL_ANN, (KERN_WARNING
  371. "megaraid: pci_enable_device failed\n"));
  372. return -ENODEV;
  373. }
  374. // Enable bus-mastering on this controller
  375. pci_set_master(pdev);
  376. // Allocate the per driver initialization structure
  377. adapter = kzalloc(sizeof(adapter_t), GFP_KERNEL);
  378. if (adapter == NULL) {
  379. con_log(CL_ANN, (KERN_WARNING
  380. "megaraid: out of memory, %s %d.\n", __func__, __LINE__));
  381. goto out_probe_one;
  382. }
  383. // set up PCI related soft state and other pre-known parameters
  384. adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
  385. adapter->irq = pdev->irq;
  386. adapter->pdev = pdev;
  387. atomic_set(&adapter->being_detached, 0);
  388. // Setup the default DMA mask. This would be changed later on
  389. // depending on hardware capabilities
  390. if (pci_set_dma_mask(adapter->pdev, DMA_BIT_MASK(32)) != 0) {
  391. con_log(CL_ANN, (KERN_WARNING
  392. "megaraid: pci_set_dma_mask failed:%d\n", __LINE__));
  393. goto out_free_adapter;
  394. }
  395. // Initialize the synchronization lock for kernel and LLD
  396. spin_lock_init(&adapter->lock);
  397. // Initialize the command queues: the list of free SCBs and the list
  398. // of pending SCBs.
  399. INIT_LIST_HEAD(&adapter->kscb_pool);
  400. spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
  401. INIT_LIST_HEAD(&adapter->pend_list);
  402. spin_lock_init(PENDING_LIST_LOCK(adapter));
  403. INIT_LIST_HEAD(&adapter->completed_list);
  404. spin_lock_init(COMPLETED_LIST_LOCK(adapter));
  405. // Start the mailbox based controller
  406. if (megaraid_init_mbox(adapter) != 0) {
  407. con_log(CL_ANN, (KERN_WARNING
  408. "megaraid: maibox adapter did not initialize\n"));
  409. goto out_free_adapter;
  410. }
  411. // Register with LSI Common Management Module
  412. if (megaraid_cmm_register(adapter) != 0) {
  413. con_log(CL_ANN, (KERN_WARNING
  414. "megaraid: could not register with management module\n"));
  415. goto out_fini_mbox;
  416. }
  417. // setup adapter handle in PCI soft state
  418. pci_set_drvdata(pdev, adapter);
  419. // attach with scsi mid-layer
  420. if (megaraid_io_attach(adapter) != 0) {
  421. con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
  422. goto out_cmm_unreg;
  423. }
  424. return 0;
  425. out_cmm_unreg:
  426. megaraid_cmm_unregister(adapter);
  427. out_fini_mbox:
  428. megaraid_fini_mbox(adapter);
  429. out_free_adapter:
  430. kfree(adapter);
  431. out_probe_one:
  432. pci_disable_device(pdev);
  433. return -ENODEV;
  434. }
  435. /**
  436. * megaraid_detach_one - release framework resources and call LLD release routine
  437. * @pdev : handle for our PCI configuration space
  438. *
  439. * This routine is called during driver unload. We free all the allocated
  440. * resources and call the corresponding LLD so that it can also release all
  441. * its resources.
  442. *
  443. * This routine is also called from the PCI hotplug system.
  444. */
  445. static void
  446. megaraid_detach_one(struct pci_dev *pdev)
  447. {
  448. adapter_t *adapter;
  449. struct Scsi_Host *host;
  450. // Start a rollback on this adapter
  451. adapter = pci_get_drvdata(pdev);
  452. if (!adapter) {
  453. con_log(CL_ANN, (KERN_CRIT
  454. "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
  455. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  456. pdev->subsystem_device));
  457. return;
  458. }
  459. else {
  460. con_log(CL_ANN, (KERN_NOTICE
  461. "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
  462. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  463. pdev->subsystem_device));
  464. }
  465. host = adapter->host;
  466. // do not allow any more requests from the management module for this
  467. // adapter.
  468. // FIXME: How do we account for the request which might still be
  469. // pending with us?
  470. atomic_set(&adapter->being_detached, 1);
  471. // detach from the IO sub-system
  472. megaraid_io_detach(adapter);
  473. // Unregister from common management module
  474. //
  475. // FIXME: this must return success or failure for conditions if there
  476. // is a command pending with LLD or not.
  477. megaraid_cmm_unregister(adapter);
  478. // finalize the mailbox based controller and release all resources
  479. megaraid_fini_mbox(adapter);
  480. kfree(adapter);
  481. scsi_host_put(host);
  482. pci_disable_device(pdev);
  483. return;
  484. }
  485. /**
  486. * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
  487. * @pdev : generic driver model device
  488. *
  489. * Shutdown notification, perform flush cache.
  490. */
  491. static void
  492. megaraid_mbox_shutdown(struct pci_dev *pdev)
  493. {
  494. adapter_t *adapter = pci_get_drvdata(pdev);
  495. static int counter;
  496. if (!adapter) {
  497. con_log(CL_ANN, (KERN_WARNING
  498. "megaraid: null device in shutdown\n"));
  499. return;
  500. }
  501. // flush caches now
  502. con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
  503. counter++));
  504. megaraid_mbox_flush_cache(adapter);
  505. con_log(CL_ANN, ("done\n"));
  506. }
  507. /**
  508. * megaraid_io_attach - attach a device with the IO subsystem
  509. * @adapter : controller's soft state
  510. *
  511. * Attach this device with the IO subsystem.
  512. */
  513. static int
  514. megaraid_io_attach(adapter_t *adapter)
  515. {
  516. struct Scsi_Host *host;
  517. // Initialize SCSI Host structure
  518. host = scsi_host_alloc(&megaraid_template_g, 8);
  519. if (!host) {
  520. con_log(CL_ANN, (KERN_WARNING
  521. "megaraid mbox: scsi_register failed\n"));
  522. return -1;
  523. }
  524. SCSIHOST2ADAP(host) = (caddr_t)adapter;
  525. adapter->host = host;
  526. host->irq = adapter->irq;
  527. host->unique_id = adapter->unique_id;
  528. host->can_queue = adapter->max_cmds;
  529. host->this_id = adapter->init_id;
  530. host->sg_tablesize = adapter->sglen;
  531. host->max_sectors = adapter->max_sectors;
  532. host->cmd_per_lun = adapter->cmd_per_lun;
  533. host->max_channel = adapter->max_channel;
  534. host->max_id = adapter->max_target;
  535. host->max_lun = adapter->max_lun;
  536. // notify mid-layer about the new controller
  537. if (scsi_add_host(host, &adapter->pdev->dev)) {
  538. con_log(CL_ANN, (KERN_WARNING
  539. "megaraid mbox: scsi_add_host failed\n"));
  540. scsi_host_put(host);
  541. return -1;
  542. }
  543. scsi_scan_host(host);
  544. return 0;
  545. }
  546. /**
  547. * megaraid_io_detach - detach a device from the IO subsystem
  548. * @adapter : controller's soft state
  549. *
  550. * Detach this device from the IO subsystem.
  551. */
  552. static void
  553. megaraid_io_detach(adapter_t *adapter)
  554. {
  555. struct Scsi_Host *host;
  556. con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
  557. host = adapter->host;
  558. scsi_remove_host(host);
  559. return;
  560. }
  561. /*
  562. * START: Mailbox Low Level Driver
  563. *
  564. * This is section specific to the single mailbox based controllers
  565. */
  566. /**
  567. * megaraid_init_mbox - initialize controller
  568. * @adapter : our soft state
  569. *
  570. * - Allocate 16-byte aligned mailbox memory for firmware handshake
  571. * - Allocate controller's memory resources
  572. * - Find out all initialization data
  573. * - Allocate memory required for all the commands
  574. * - Use internal library of FW routines, build up complete soft state
  575. */
  576. static int
  577. megaraid_init_mbox(adapter_t *adapter)
  578. {
  579. struct pci_dev *pdev;
  580. mraid_device_t *raid_dev;
  581. int i;
  582. uint32_t magic64;
  583. adapter->ito = MBOX_TIMEOUT;
  584. pdev = adapter->pdev;
  585. /*
  586. * Allocate and initialize the init data structure for mailbox
  587. * controllers
  588. */
  589. raid_dev = kzalloc(sizeof(mraid_device_t), GFP_KERNEL);
  590. if (raid_dev == NULL) return -1;
  591. /*
  592. * Attach the adapter soft state to raid device soft state
  593. */
  594. adapter->raid_device = (caddr_t)raid_dev;
  595. raid_dev->fast_load = megaraid_fast_load;
  596. // our baseport
  597. raid_dev->baseport = pci_resource_start(pdev, 0);
  598. if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
  599. con_log(CL_ANN, (KERN_WARNING
  600. "megaraid: mem region busy\n"));
  601. goto out_free_raid_dev;
  602. }
  603. raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
  604. if (!raid_dev->baseaddr) {
  605. con_log(CL_ANN, (KERN_WARNING
  606. "megaraid: could not map hba memory\n") );
  607. goto out_release_regions;
  608. }
  609. /* initialize the mutual exclusion lock for the mailbox */
  610. spin_lock_init(&raid_dev->mailbox_lock);
  611. /* allocate memory required for commands */
  612. if (megaraid_alloc_cmd_packets(adapter) != 0)
  613. goto out_iounmap;
  614. /*
  615. * Issue SYNC cmd to flush the pending cmds in the adapter
  616. * and initialize its internal state
  617. */
  618. if (megaraid_mbox_fire_sync_cmd(adapter))
  619. con_log(CL_ANN, ("megaraid: sync cmd failed\n"));
  620. /*
  621. * Setup the rest of the soft state using the library of
  622. * FW routines
  623. */
  624. /* request IRQ and register the interrupt service routine */
  625. if (request_irq(adapter->irq, megaraid_isr, IRQF_SHARED, "megaraid",
  626. adapter)) {
  627. con_log(CL_ANN, (KERN_WARNING
  628. "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
  629. goto out_alloc_cmds;
  630. }
  631. // Product info
  632. if (megaraid_mbox_product_info(adapter) != 0)
  633. goto out_free_irq;
  634. // Do we support extended CDBs
  635. adapter->max_cdb_sz = 10;
  636. if (megaraid_mbox_extended_cdb(adapter) == 0) {
  637. adapter->max_cdb_sz = 16;
  638. }
  639. /*
  640. * Do we support cluster environment, if we do, what is the initiator
  641. * id.
  642. * NOTE: In a non-cluster aware firmware environment, the LLD should
  643. * return 7 as initiator id.
  644. */
  645. adapter->ha = 0;
  646. adapter->init_id = -1;
  647. if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
  648. adapter->ha = 1;
  649. }
  650. /*
  651. * Prepare the device ids array to have the mapping between the kernel
  652. * device address and megaraid device address.
  653. * We export the physical devices on their actual addresses. The
  654. * logical drives are exported on a virtual SCSI channel
  655. */
  656. megaraid_mbox_setup_device_map(adapter);
  657. // If the firmware supports random deletion, update the device id map
  658. if (megaraid_mbox_support_random_del(adapter)) {
  659. // Change the logical drives numbers in device_ids array one
  660. // slot in device_ids is reserved for target id, that's why
  661. // "<=" below
  662. for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
  663. adapter->device_ids[adapter->max_channel][i] += 0x80;
  664. }
  665. adapter->device_ids[adapter->max_channel][adapter->init_id] =
  666. 0xFF;
  667. raid_dev->random_del_supported = 1;
  668. }
  669. /*
  670. * find out the maximum number of scatter-gather elements supported by
  671. * this firmware
  672. */
  673. adapter->sglen = megaraid_mbox_get_max_sg(adapter);
  674. // enumerate RAID and SCSI channels so that all devices on SCSI
  675. // channels can later be exported, including disk devices
  676. megaraid_mbox_enum_raid_scsi(adapter);
  677. /*
  678. * Other parameters required by upper layer
  679. *
  680. * maximum number of sectors per IO command
  681. */
  682. adapter->max_sectors = megaraid_max_sectors;
  683. /*
  684. * number of queued commands per LUN.
  685. */
  686. adapter->cmd_per_lun = megaraid_cmd_per_lun;
  687. /*
  688. * Allocate resources required to issue FW calls, when sysfs is
  689. * accessed
  690. */
  691. if (megaraid_sysfs_alloc_resources(adapter) != 0)
  692. goto out_free_irq;
  693. // Set the DMA mask to 64-bit. All supported controllers as capable of
  694. // DMA in this range
  695. pci_read_config_dword(adapter->pdev, PCI_CONF_AMISIG64, &magic64);
  696. if (((magic64 == HBA_SIGNATURE_64_BIT) &&
  697. ((adapter->pdev->subsystem_device !=
  698. PCI_SUBSYS_ID_MEGARAID_SATA_150_6) &&
  699. (adapter->pdev->subsystem_device !=
  700. PCI_SUBSYS_ID_MEGARAID_SATA_150_4))) ||
  701. (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
  702. adapter->pdev->device == PCI_DEVICE_ID_VERDE) ||
  703. (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
  704. adapter->pdev->device == PCI_DEVICE_ID_DOBSON) ||
  705. (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
  706. adapter->pdev->device == PCI_DEVICE_ID_LINDSAY) ||
  707. (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
  708. adapter->pdev->device == PCI_DEVICE_ID_PERC4_DI_EVERGLADES) ||
  709. (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
  710. adapter->pdev->device == PCI_DEVICE_ID_PERC4E_DI_KOBUK)) {
  711. if (pci_set_dma_mask(adapter->pdev, DMA_BIT_MASK(64))) {
  712. con_log(CL_ANN, (KERN_WARNING
  713. "megaraid: DMA mask for 64-bit failed\n"));
  714. if (pci_set_dma_mask (adapter->pdev, DMA_BIT_MASK(32))) {
  715. con_log(CL_ANN, (KERN_WARNING
  716. "megaraid: 32-bit DMA mask failed\n"));
  717. goto out_free_sysfs_res;
  718. }
  719. }
  720. }
  721. // setup tasklet for DPC
  722. tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
  723. (unsigned long)adapter);
  724. con_log(CL_DLEVEL1, (KERN_INFO
  725. "megaraid mbox hba successfully initialized\n"));
  726. return 0;
  727. out_free_sysfs_res:
  728. megaraid_sysfs_free_resources(adapter);
  729. out_free_irq:
  730. free_irq(adapter->irq, adapter);
  731. out_alloc_cmds:
  732. megaraid_free_cmd_packets(adapter);
  733. out_iounmap:
  734. iounmap(raid_dev->baseaddr);
  735. out_release_regions:
  736. pci_release_regions(pdev);
  737. out_free_raid_dev:
  738. kfree(raid_dev);
  739. return -1;
  740. }
  741. /**
  742. * megaraid_fini_mbox - undo controller initialization
  743. * @adapter : our soft state
  744. */
  745. static void
  746. megaraid_fini_mbox(adapter_t *adapter)
  747. {
  748. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  749. // flush all caches
  750. megaraid_mbox_flush_cache(adapter);
  751. tasklet_kill(&adapter->dpc_h);
  752. megaraid_sysfs_free_resources(adapter);
  753. megaraid_free_cmd_packets(adapter);
  754. free_irq(adapter->irq, adapter);
  755. iounmap(raid_dev->baseaddr);
  756. pci_release_regions(adapter->pdev);
  757. kfree(raid_dev);
  758. return;
  759. }
  760. /**
  761. * megaraid_alloc_cmd_packets - allocate shared mailbox
  762. * @adapter : soft state of the raid controller
  763. *
  764. * Allocate and align the shared mailbox. This maibox is used to issue
  765. * all the commands. For IO based controllers, the mailbox is also registered
  766. * with the FW. Allocate memory for all commands as well.
  767. * This is our big allocator.
  768. */
  769. static int
  770. megaraid_alloc_cmd_packets(adapter_t *adapter)
  771. {
  772. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  773. struct pci_dev *pdev;
  774. unsigned long align;
  775. scb_t *scb;
  776. mbox_ccb_t *ccb;
  777. struct mraid_pci_blk *epthru_pci_blk;
  778. struct mraid_pci_blk *sg_pci_blk;
  779. struct mraid_pci_blk *mbox_pci_blk;
  780. int i;
  781. pdev = adapter->pdev;
  782. /*
  783. * Setup the mailbox
  784. * Allocate the common 16-byte aligned memory for the handshake
  785. * mailbox.
  786. */
  787. raid_dev->una_mbox64 = pci_zalloc_consistent(adapter->pdev,
  788. sizeof(mbox64_t),
  789. &raid_dev->una_mbox64_dma);
  790. if (!raid_dev->una_mbox64) {
  791. con_log(CL_ANN, (KERN_WARNING
  792. "megaraid: out of memory, %s %d\n", __func__,
  793. __LINE__));
  794. return -1;
  795. }
  796. /*
  797. * Align the mailbox at 16-byte boundary
  798. */
  799. raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
  800. raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
  801. (~0UL ^ 0xFUL));
  802. raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
  803. align = ((void *)raid_dev->mbox -
  804. ((void *)&raid_dev->una_mbox64->mbox32));
  805. raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
  806. align;
  807. // Allocate memory for commands issued internally
  808. adapter->ibuf = pci_zalloc_consistent(pdev, MBOX_IBUF_SIZE,
  809. &adapter->ibuf_dma_h);
  810. if (!adapter->ibuf) {
  811. con_log(CL_ANN, (KERN_WARNING
  812. "megaraid: out of memory, %s %d\n", __func__,
  813. __LINE__));
  814. goto out_free_common_mbox;
  815. }
  816. // Allocate memory for our SCSI Command Blocks and their associated
  817. // memory
  818. /*
  819. * Allocate memory for the base list of scb. Later allocate memory for
  820. * CCBs and embedded components of each CCB and point the pointers in
  821. * scb to the allocated components
  822. * NOTE: The code to allocate SCB will be duplicated in all the LLD
  823. * since the calling routine does not yet know the number of available
  824. * commands.
  825. */
  826. adapter->kscb_list = kcalloc(MBOX_MAX_SCSI_CMDS, sizeof(scb_t), GFP_KERNEL);
  827. if (adapter->kscb_list == NULL) {
  828. con_log(CL_ANN, (KERN_WARNING
  829. "megaraid: out of memory, %s %d\n", __func__,
  830. __LINE__));
  831. goto out_free_ibuf;
  832. }
  833. // memory allocation for our command packets
  834. if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
  835. con_log(CL_ANN, (KERN_WARNING
  836. "megaraid: out of memory, %s %d\n", __func__,
  837. __LINE__));
  838. goto out_free_scb_list;
  839. }
  840. // Adjust the scb pointers and link in the free pool
  841. epthru_pci_blk = raid_dev->epthru_pool;
  842. sg_pci_blk = raid_dev->sg_pool;
  843. mbox_pci_blk = raid_dev->mbox_pool;
  844. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  845. scb = adapter->kscb_list + i;
  846. ccb = raid_dev->ccb_list + i;
  847. ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
  848. ccb->raw_mbox = (uint8_t *)ccb->mbox;
  849. ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
  850. ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
  851. // make sure the mailbox is aligned properly
  852. if (ccb->mbox_dma_h & 0x0F) {
  853. con_log(CL_ANN, (KERN_CRIT
  854. "megaraid mbox: not aligned on 16-bytes\n"));
  855. goto out_teardown_dma_pools;
  856. }
  857. ccb->epthru = (mraid_epassthru_t *)
  858. epthru_pci_blk[i].vaddr;
  859. ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
  860. ccb->pthru = (mraid_passthru_t *)ccb->epthru;
  861. ccb->pthru_dma_h = ccb->epthru_dma_h;
  862. ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
  863. ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
  864. ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
  865. scb->ccb = (caddr_t)ccb;
  866. scb->gp = 0;
  867. scb->sno = i; // command index
  868. scb->scp = NULL;
  869. scb->state = SCB_FREE;
  870. scb->dma_direction = PCI_DMA_NONE;
  871. scb->dma_type = MRAID_DMA_NONE;
  872. scb->dev_channel = -1;
  873. scb->dev_target = -1;
  874. // put scb in the free pool
  875. list_add_tail(&scb->list, &adapter->kscb_pool);
  876. }
  877. return 0;
  878. out_teardown_dma_pools:
  879. megaraid_mbox_teardown_dma_pools(adapter);
  880. out_free_scb_list:
  881. kfree(adapter->kscb_list);
  882. out_free_ibuf:
  883. pci_free_consistent(pdev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
  884. adapter->ibuf_dma_h);
  885. out_free_common_mbox:
  886. pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
  887. (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
  888. return -1;
  889. }
  890. /**
  891. * megaraid_free_cmd_packets - free memory
  892. * @adapter : soft state of the raid controller
  893. *
  894. * Release memory resources allocated for commands.
  895. */
  896. static void
  897. megaraid_free_cmd_packets(adapter_t *adapter)
  898. {
  899. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  900. megaraid_mbox_teardown_dma_pools(adapter);
  901. kfree(adapter->kscb_list);
  902. pci_free_consistent(adapter->pdev, MBOX_IBUF_SIZE,
  903. (void *)adapter->ibuf, adapter->ibuf_dma_h);
  904. pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
  905. (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
  906. return;
  907. }
  908. /**
  909. * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
  910. * @adapter : HBA soft state
  911. *
  912. * Setup the dma pools for mailbox, passthru and extended passthru structures,
  913. * and scatter-gather lists.
  914. */
  915. static int
  916. megaraid_mbox_setup_dma_pools(adapter_t *adapter)
  917. {
  918. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  919. struct mraid_pci_blk *epthru_pci_blk;
  920. struct mraid_pci_blk *sg_pci_blk;
  921. struct mraid_pci_blk *mbox_pci_blk;
  922. int i;
  923. // Allocate memory for 16-bytes aligned mailboxes
  924. raid_dev->mbox_pool_handle = dma_pool_create("megaraid mbox pool",
  925. &adapter->pdev->dev,
  926. sizeof(mbox64_t) + 16,
  927. 16, 0);
  928. if (raid_dev->mbox_pool_handle == NULL) {
  929. goto fail_setup_dma_pool;
  930. }
  931. mbox_pci_blk = raid_dev->mbox_pool;
  932. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  933. mbox_pci_blk[i].vaddr = dma_pool_alloc(
  934. raid_dev->mbox_pool_handle,
  935. GFP_KERNEL,
  936. &mbox_pci_blk[i].dma_addr);
  937. if (!mbox_pci_blk[i].vaddr) {
  938. goto fail_setup_dma_pool;
  939. }
  940. }
  941. /*
  942. * Allocate memory for each embedded passthru strucuture pointer
  943. * Request for a 128 bytes aligned structure for each passthru command
  944. * structure
  945. * Since passthru and extended passthru commands are exclusive, they
  946. * share common memory pool. Passthru structures piggyback on memory
  947. * allocted to extended passthru since passthru is smaller of the two
  948. */
  949. raid_dev->epthru_pool_handle = dma_pool_create("megaraid mbox pthru",
  950. &adapter->pdev->dev, sizeof(mraid_epassthru_t), 128, 0);
  951. if (raid_dev->epthru_pool_handle == NULL) {
  952. goto fail_setup_dma_pool;
  953. }
  954. epthru_pci_blk = raid_dev->epthru_pool;
  955. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  956. epthru_pci_blk[i].vaddr = dma_pool_alloc(
  957. raid_dev->epthru_pool_handle,
  958. GFP_KERNEL,
  959. &epthru_pci_blk[i].dma_addr);
  960. if (!epthru_pci_blk[i].vaddr) {
  961. goto fail_setup_dma_pool;
  962. }
  963. }
  964. // Allocate memory for each scatter-gather list. Request for 512 bytes
  965. // alignment for each sg list
  966. raid_dev->sg_pool_handle = dma_pool_create("megaraid mbox sg",
  967. &adapter->pdev->dev,
  968. sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
  969. 512, 0);
  970. if (raid_dev->sg_pool_handle == NULL) {
  971. goto fail_setup_dma_pool;
  972. }
  973. sg_pci_blk = raid_dev->sg_pool;
  974. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  975. sg_pci_blk[i].vaddr = dma_pool_alloc(
  976. raid_dev->sg_pool_handle,
  977. GFP_KERNEL,
  978. &sg_pci_blk[i].dma_addr);
  979. if (!sg_pci_blk[i].vaddr) {
  980. goto fail_setup_dma_pool;
  981. }
  982. }
  983. return 0;
  984. fail_setup_dma_pool:
  985. megaraid_mbox_teardown_dma_pools(adapter);
  986. return -1;
  987. }
  988. /**
  989. * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
  990. * @adapter : HBA soft state
  991. *
  992. * Teardown the dma pool for mailbox, passthru and extended passthru
  993. * structures, and scatter-gather lists.
  994. */
  995. static void
  996. megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
  997. {
  998. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  999. struct mraid_pci_blk *epthru_pci_blk;
  1000. struct mraid_pci_blk *sg_pci_blk;
  1001. struct mraid_pci_blk *mbox_pci_blk;
  1002. int i;
  1003. sg_pci_blk = raid_dev->sg_pool;
  1004. for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
  1005. dma_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
  1006. sg_pci_blk[i].dma_addr);
  1007. }
  1008. if (raid_dev->sg_pool_handle)
  1009. dma_pool_destroy(raid_dev->sg_pool_handle);
  1010. epthru_pci_blk = raid_dev->epthru_pool;
  1011. for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
  1012. dma_pool_free(raid_dev->epthru_pool_handle,
  1013. epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
  1014. }
  1015. if (raid_dev->epthru_pool_handle)
  1016. dma_pool_destroy(raid_dev->epthru_pool_handle);
  1017. mbox_pci_blk = raid_dev->mbox_pool;
  1018. for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
  1019. dma_pool_free(raid_dev->mbox_pool_handle,
  1020. mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
  1021. }
  1022. if (raid_dev->mbox_pool_handle)
  1023. dma_pool_destroy(raid_dev->mbox_pool_handle);
  1024. return;
  1025. }
  1026. /**
  1027. * megaraid_alloc_scb - detach and return a scb from the free list
  1028. * @adapter : controller's soft state
  1029. * @scp : pointer to the scsi command to be executed
  1030. *
  1031. * Return the scb from the head of the free list. %NULL if there are none
  1032. * available.
  1033. */
  1034. static scb_t *
  1035. megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
  1036. {
  1037. struct list_head *head = &adapter->kscb_pool;
  1038. scb_t *scb = NULL;
  1039. unsigned long flags;
  1040. // detach scb from free pool
  1041. spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
  1042. if (list_empty(head)) {
  1043. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1044. return NULL;
  1045. }
  1046. scb = list_entry(head->next, scb_t, list);
  1047. list_del_init(&scb->list);
  1048. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1049. scb->state = SCB_ACTIVE;
  1050. scb->scp = scp;
  1051. scb->dma_type = MRAID_DMA_NONE;
  1052. return scb;
  1053. }
  1054. /**
  1055. * megaraid_dealloc_scb - return the scb to the free pool
  1056. * @adapter : controller's soft state
  1057. * @scb : scb to be freed
  1058. *
  1059. * Return the scb back to the free list of scbs. The caller must 'flush' the
  1060. * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
  1061. * NOTE NOTE: Make sure the scb is not on any list before calling this
  1062. * routine.
  1063. */
  1064. static inline void
  1065. megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
  1066. {
  1067. unsigned long flags;
  1068. // put scb in the free pool
  1069. scb->state = SCB_FREE;
  1070. scb->scp = NULL;
  1071. spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
  1072. list_add(&scb->list, &adapter->kscb_pool);
  1073. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1074. return;
  1075. }
  1076. /**
  1077. * megaraid_mbox_mksgl - make the scatter-gather list
  1078. * @adapter : controller's soft state
  1079. * @scb : scsi control block
  1080. *
  1081. * Prepare the scatter-gather list.
  1082. */
  1083. static int
  1084. megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
  1085. {
  1086. struct scatterlist *sgl;
  1087. mbox_ccb_t *ccb;
  1088. struct scsi_cmnd *scp;
  1089. int sgcnt;
  1090. int i;
  1091. scp = scb->scp;
  1092. ccb = (mbox_ccb_t *)scb->ccb;
  1093. sgcnt = scsi_dma_map(scp);
  1094. BUG_ON(sgcnt < 0 || sgcnt > adapter->sglen);
  1095. // no mapping required if no data to be transferred
  1096. if (!sgcnt)
  1097. return 0;
  1098. scb->dma_type = MRAID_DMA_WSG;
  1099. scsi_for_each_sg(scp, sgl, sgcnt, i) {
  1100. ccb->sgl64[i].address = sg_dma_address(sgl);
  1101. ccb->sgl64[i].length = sg_dma_len(sgl);
  1102. }
  1103. // Return count of SG nodes
  1104. return sgcnt;
  1105. }
  1106. /**
  1107. * mbox_post_cmd - issue a mailbox command
  1108. * @adapter : controller's soft state
  1109. * @scb : command to be issued
  1110. *
  1111. * Post the command to the controller if mailbox is available.
  1112. */
  1113. static int
  1114. mbox_post_cmd(adapter_t *adapter, scb_t *scb)
  1115. {
  1116. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  1117. mbox64_t *mbox64;
  1118. mbox_t *mbox;
  1119. mbox_ccb_t *ccb;
  1120. unsigned long flags;
  1121. unsigned int i = 0;
  1122. ccb = (mbox_ccb_t *)scb->ccb;
  1123. mbox = raid_dev->mbox;
  1124. mbox64 = raid_dev->mbox64;
  1125. /*
  1126. * Check for busy mailbox. If it is, return failure - the caller
  1127. * should retry later.
  1128. */
  1129. spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
  1130. if (unlikely(mbox->busy)) {
  1131. do {
  1132. udelay(1);
  1133. i++;
  1134. rmb();
  1135. } while(mbox->busy && (i < max_mbox_busy_wait));
  1136. if (mbox->busy) {
  1137. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1138. return -1;
  1139. }
  1140. }
  1141. // Copy this command's mailbox data into "adapter's" mailbox
  1142. memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
  1143. mbox->cmdid = scb->sno;
  1144. adapter->outstanding_cmds++;
  1145. if (scb->dma_direction == PCI_DMA_TODEVICE)
  1146. pci_dma_sync_sg_for_device(adapter->pdev,
  1147. scsi_sglist(scb->scp),
  1148. scsi_sg_count(scb->scp),
  1149. PCI_DMA_TODEVICE);
  1150. mbox->busy = 1; // Set busy
  1151. mbox->poll = 0;
  1152. mbox->ack = 0;
  1153. wmb();
  1154. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  1155. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1156. return 0;
  1157. }
  1158. /**
  1159. * megaraid_queue_command - generic queue entry point for all LLDs
  1160. * @scp : pointer to the scsi command to be executed
  1161. * @done : callback routine to be called after the cmd has be completed
  1162. *
  1163. * Queue entry point for mailbox based controllers.
  1164. */
  1165. static int
  1166. megaraid_queue_command_lck(struct scsi_cmnd *scp, void (*done)(struct scsi_cmnd *))
  1167. {
  1168. adapter_t *adapter;
  1169. scb_t *scb;
  1170. int if_busy;
  1171. adapter = SCP2ADAPTER(scp);
  1172. scp->scsi_done = done;
  1173. scp->result = 0;
  1174. /*
  1175. * Allocate and build a SCB request
  1176. * if_busy flag will be set if megaraid_mbox_build_cmd() command could
  1177. * not allocate scb. We will return non-zero status in that case.
  1178. * NOTE: scb can be null even though certain commands completed
  1179. * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
  1180. * return 0 in that case, and we would do the callback right away.
  1181. */
  1182. if_busy = 0;
  1183. scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
  1184. if (!scb) { // command already completed
  1185. done(scp);
  1186. return 0;
  1187. }
  1188. megaraid_mbox_runpendq(adapter, scb);
  1189. return if_busy;
  1190. }
  1191. static DEF_SCSI_QCMD(megaraid_queue_command)
  1192. /**
  1193. * megaraid_mbox_build_cmd - transform the mid-layer scsi commands
  1194. * @adapter : controller's soft state
  1195. * @scp : mid-layer scsi command pointer
  1196. * @busy : set if request could not be completed because of lack of
  1197. * resources
  1198. *
  1199. * Transform the mid-layer scsi command to megaraid firmware lingua.
  1200. * Convert the command issued by mid-layer to format understood by megaraid
  1201. * firmware. We also complete certain commands without sending them to firmware.
  1202. */
  1203. static scb_t *
  1204. megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
  1205. {
  1206. mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
  1207. int channel;
  1208. int target;
  1209. int islogical;
  1210. mbox_ccb_t *ccb;
  1211. mraid_passthru_t *pthru;
  1212. mbox64_t *mbox64;
  1213. mbox_t *mbox;
  1214. scb_t *scb;
  1215. char skip[] = "skipping";
  1216. char scan[] = "scanning";
  1217. char *ss;
  1218. /*
  1219. * Get the appropriate device map for the device this command is
  1220. * intended for
  1221. */
  1222. MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
  1223. /*
  1224. * Logical drive commands
  1225. */
  1226. if (islogical) {
  1227. switch (scp->cmnd[0]) {
  1228. case TEST_UNIT_READY:
  1229. /*
  1230. * Do we support clustering and is the support enabled
  1231. * If no, return success always
  1232. */
  1233. if (!adapter->ha) {
  1234. scp->result = (DID_OK << 16);
  1235. return NULL;
  1236. }
  1237. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1238. scp->result = (DID_ERROR << 16);
  1239. *busy = 1;
  1240. return NULL;
  1241. }
  1242. scb->dma_direction = scp->sc_data_direction;
  1243. scb->dev_channel = 0xFF;
  1244. scb->dev_target = target;
  1245. ccb = (mbox_ccb_t *)scb->ccb;
  1246. /*
  1247. * The command id will be provided by the command
  1248. * issuance routine
  1249. */
  1250. ccb->raw_mbox[0] = CLUSTER_CMD;
  1251. ccb->raw_mbox[2] = RESERVATION_STATUS;
  1252. ccb->raw_mbox[3] = target;
  1253. return scb;
  1254. case MODE_SENSE:
  1255. {
  1256. struct scatterlist *sgl;
  1257. caddr_t vaddr;
  1258. sgl = scsi_sglist(scp);
  1259. if (sg_page(sgl)) {
  1260. vaddr = (caddr_t) sg_virt(&sgl[0]);
  1261. memset(vaddr, 0, scp->cmnd[4]);
  1262. }
  1263. else {
  1264. con_log(CL_ANN, (KERN_WARNING
  1265. "megaraid mailbox: invalid sg:%d\n",
  1266. __LINE__));
  1267. }
  1268. }
  1269. scp->result = (DID_OK << 16);
  1270. return NULL;
  1271. case INQUIRY:
  1272. /*
  1273. * Display the channel scan for logical drives
  1274. * Do not display scan for a channel if already done.
  1275. */
  1276. if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
  1277. con_log(CL_ANN, (KERN_INFO
  1278. "scsi[%d]: scanning scsi channel %d",
  1279. adapter->host->host_no,
  1280. SCP2CHANNEL(scp)));
  1281. con_log(CL_ANN, (
  1282. " [virtual] for logical drives\n"));
  1283. rdev->last_disp |= (1L << SCP2CHANNEL(scp));
  1284. }
  1285. if (scp->cmnd[1] & MEGA_SCSI_INQ_EVPD) {
  1286. scp->sense_buffer[0] = 0x70;
  1287. scp->sense_buffer[2] = ILLEGAL_REQUEST;
  1288. scp->sense_buffer[12] = MEGA_INVALID_FIELD_IN_CDB;
  1289. scp->result = CHECK_CONDITION << 1;
  1290. return NULL;
  1291. }
  1292. /* Fall through */
  1293. case READ_CAPACITY:
  1294. /*
  1295. * Do not allow LUN > 0 for logical drives and
  1296. * requests for more than 40 logical drives
  1297. */
  1298. if (SCP2LUN(scp)) {
  1299. scp->result = (DID_BAD_TARGET << 16);
  1300. return NULL;
  1301. }
  1302. if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
  1303. scp->result = (DID_BAD_TARGET << 16);
  1304. return NULL;
  1305. }
  1306. /* Allocate a SCB and initialize passthru */
  1307. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1308. scp->result = (DID_ERROR << 16);
  1309. *busy = 1;
  1310. return NULL;
  1311. }
  1312. ccb = (mbox_ccb_t *)scb->ccb;
  1313. scb->dev_channel = 0xFF;
  1314. scb->dev_target = target;
  1315. pthru = ccb->pthru;
  1316. mbox = ccb->mbox;
  1317. mbox64 = ccb->mbox64;
  1318. pthru->timeout = 0;
  1319. pthru->ars = 1;
  1320. pthru->reqsenselen = 14;
  1321. pthru->islogical = 1;
  1322. pthru->logdrv = target;
  1323. pthru->cdblen = scp->cmd_len;
  1324. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  1325. mbox->cmd = MBOXCMD_PASSTHRU64;
  1326. scb->dma_direction = scp->sc_data_direction;
  1327. pthru->dataxferlen = scsi_bufflen(scp);
  1328. pthru->dataxferaddr = ccb->sgl_dma_h;
  1329. pthru->numsge = megaraid_mbox_mksgl(adapter,
  1330. scb);
  1331. mbox->xferaddr = 0xFFFFFFFF;
  1332. mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
  1333. mbox64->xferaddr_hi = 0;
  1334. return scb;
  1335. case READ_6:
  1336. case WRITE_6:
  1337. case READ_10:
  1338. case WRITE_10:
  1339. case READ_12:
  1340. case WRITE_12:
  1341. /*
  1342. * Allocate a SCB and initialize mailbox
  1343. */
  1344. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1345. scp->result = (DID_ERROR << 16);
  1346. *busy = 1;
  1347. return NULL;
  1348. }
  1349. ccb = (mbox_ccb_t *)scb->ccb;
  1350. scb->dev_channel = 0xFF;
  1351. scb->dev_target = target;
  1352. mbox = ccb->mbox;
  1353. mbox64 = ccb->mbox64;
  1354. mbox->logdrv = target;
  1355. /*
  1356. * A little HACK: 2nd bit is zero for all scsi read
  1357. * commands and is set for all scsi write commands
  1358. */
  1359. mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
  1360. MBOXCMD_LREAD64 ;
  1361. /*
  1362. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  1363. */
  1364. if (scp->cmd_len == 6) {
  1365. mbox->numsectors = (uint32_t)scp->cmnd[4];
  1366. mbox->lba =
  1367. ((uint32_t)scp->cmnd[1] << 16) |
  1368. ((uint32_t)scp->cmnd[2] << 8) |
  1369. (uint32_t)scp->cmnd[3];
  1370. mbox->lba &= 0x1FFFFF;
  1371. }
  1372. /*
  1373. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  1374. */
  1375. else if (scp->cmd_len == 10) {
  1376. mbox->numsectors =
  1377. (uint32_t)scp->cmnd[8] |
  1378. ((uint32_t)scp->cmnd[7] << 8);
  1379. mbox->lba =
  1380. ((uint32_t)scp->cmnd[2] << 24) |
  1381. ((uint32_t)scp->cmnd[3] << 16) |
  1382. ((uint32_t)scp->cmnd[4] << 8) |
  1383. (uint32_t)scp->cmnd[5];
  1384. }
  1385. /*
  1386. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  1387. */
  1388. else if (scp->cmd_len == 12) {
  1389. mbox->lba =
  1390. ((uint32_t)scp->cmnd[2] << 24) |
  1391. ((uint32_t)scp->cmnd[3] << 16) |
  1392. ((uint32_t)scp->cmnd[4] << 8) |
  1393. (uint32_t)scp->cmnd[5];
  1394. mbox->numsectors =
  1395. ((uint32_t)scp->cmnd[6] << 24) |
  1396. ((uint32_t)scp->cmnd[7] << 16) |
  1397. ((uint32_t)scp->cmnd[8] << 8) |
  1398. (uint32_t)scp->cmnd[9];
  1399. }
  1400. else {
  1401. con_log(CL_ANN, (KERN_WARNING
  1402. "megaraid: unsupported CDB length\n"));
  1403. megaraid_dealloc_scb(adapter, scb);
  1404. scp->result = (DID_ERROR << 16);
  1405. return NULL;
  1406. }
  1407. scb->dma_direction = scp->sc_data_direction;
  1408. // Calculate Scatter-Gather info
  1409. mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
  1410. mbox->numsge = megaraid_mbox_mksgl(adapter,
  1411. scb);
  1412. mbox->xferaddr = 0xFFFFFFFF;
  1413. mbox64->xferaddr_hi = 0;
  1414. return scb;
  1415. case RESERVE:
  1416. case RELEASE:
  1417. /*
  1418. * Do we support clustering and is the support enabled
  1419. */
  1420. if (!adapter->ha) {
  1421. scp->result = (DID_BAD_TARGET << 16);
  1422. return NULL;
  1423. }
  1424. /*
  1425. * Allocate a SCB and initialize mailbox
  1426. */
  1427. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1428. scp->result = (DID_ERROR << 16);
  1429. *busy = 1;
  1430. return NULL;
  1431. }
  1432. ccb = (mbox_ccb_t *)scb->ccb;
  1433. scb->dev_channel = 0xFF;
  1434. scb->dev_target = target;
  1435. ccb->raw_mbox[0] = CLUSTER_CMD;
  1436. ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
  1437. RESERVE_LD : RELEASE_LD;
  1438. ccb->raw_mbox[3] = target;
  1439. scb->dma_direction = scp->sc_data_direction;
  1440. return scb;
  1441. default:
  1442. scp->result = (DID_BAD_TARGET << 16);
  1443. return NULL;
  1444. }
  1445. }
  1446. else { // Passthru device commands
  1447. // Do not allow access to target id > 15 or LUN > 7
  1448. if (target > 15 || SCP2LUN(scp) > 7) {
  1449. scp->result = (DID_BAD_TARGET << 16);
  1450. return NULL;
  1451. }
  1452. // if fast load option was set and scan for last device is
  1453. // over, reset the fast_load flag so that during a possible
  1454. // next scan, devices can be made available
  1455. if (rdev->fast_load && (target == 15) &&
  1456. (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
  1457. con_log(CL_ANN, (KERN_INFO
  1458. "megaraid[%d]: physical device scan re-enabled\n",
  1459. adapter->host->host_no));
  1460. rdev->fast_load = 0;
  1461. }
  1462. /*
  1463. * Display the channel scan for physical devices
  1464. */
  1465. if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
  1466. ss = rdev->fast_load ? skip : scan;
  1467. con_log(CL_ANN, (KERN_INFO
  1468. "scsi[%d]: %s scsi channel %d [Phy %d]",
  1469. adapter->host->host_no, ss, SCP2CHANNEL(scp),
  1470. channel));
  1471. con_log(CL_ANN, (
  1472. " for non-raid devices\n"));
  1473. rdev->last_disp |= (1L << SCP2CHANNEL(scp));
  1474. }
  1475. // disable channel sweep if fast load option given
  1476. if (rdev->fast_load) {
  1477. scp->result = (DID_BAD_TARGET << 16);
  1478. return NULL;
  1479. }
  1480. // Allocate a SCB and initialize passthru
  1481. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1482. scp->result = (DID_ERROR << 16);
  1483. *busy = 1;
  1484. return NULL;
  1485. }
  1486. ccb = (mbox_ccb_t *)scb->ccb;
  1487. scb->dev_channel = channel;
  1488. scb->dev_target = target;
  1489. scb->dma_direction = scp->sc_data_direction;
  1490. mbox = ccb->mbox;
  1491. mbox64 = ccb->mbox64;
  1492. // Does this firmware support extended CDBs
  1493. if (adapter->max_cdb_sz == 16) {
  1494. mbox->cmd = MBOXCMD_EXTPTHRU;
  1495. megaraid_mbox_prepare_epthru(adapter, scb, scp);
  1496. mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
  1497. mbox64->xferaddr_hi = 0;
  1498. mbox->xferaddr = 0xFFFFFFFF;
  1499. }
  1500. else {
  1501. mbox->cmd = MBOXCMD_PASSTHRU64;
  1502. megaraid_mbox_prepare_pthru(adapter, scb, scp);
  1503. mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
  1504. mbox64->xferaddr_hi = 0;
  1505. mbox->xferaddr = 0xFFFFFFFF;
  1506. }
  1507. return scb;
  1508. }
  1509. // NOT REACHED
  1510. }
  1511. /**
  1512. * megaraid_mbox_runpendq - execute commands queued in the pending queue
  1513. * @adapter : controller's soft state
  1514. * @scb_q : SCB to be queued in the pending list
  1515. *
  1516. * Scan the pending list for commands which are not yet issued and try to
  1517. * post to the controller. The SCB can be a null pointer, which would indicate
  1518. * no SCB to be queue, just try to execute the ones in the pending list.
  1519. *
  1520. * NOTE: We do not actually traverse the pending list. The SCBs are plucked
  1521. * out from the head of the pending list. If it is successfully issued, the
  1522. * next SCB is at the head now.
  1523. */
  1524. static void
  1525. megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
  1526. {
  1527. scb_t *scb;
  1528. unsigned long flags;
  1529. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1530. if (scb_q) {
  1531. scb_q->state = SCB_PENDQ;
  1532. list_add_tail(&scb_q->list, &adapter->pend_list);
  1533. }
  1534. // if the adapter in not in quiescent mode, post the commands to FW
  1535. if (adapter->quiescent) {
  1536. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1537. return;
  1538. }
  1539. while (!list_empty(&adapter->pend_list)) {
  1540. assert_spin_locked(PENDING_LIST_LOCK(adapter));
  1541. scb = list_entry(adapter->pend_list.next, scb_t, list);
  1542. // remove the scb from the pending list and try to
  1543. // issue. If we are unable to issue it, put back in
  1544. // the pending list and return
  1545. list_del_init(&scb->list);
  1546. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1547. // if mailbox was busy, return SCB back to pending
  1548. // list. Make sure to add at the head, since that's
  1549. // where it would have been removed from
  1550. scb->state = SCB_ISSUED;
  1551. if (mbox_post_cmd(adapter, scb) != 0) {
  1552. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1553. scb->state = SCB_PENDQ;
  1554. list_add(&scb->list, &adapter->pend_list);
  1555. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
  1556. flags);
  1557. return;
  1558. }
  1559. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1560. }
  1561. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1562. return;
  1563. }
  1564. /**
  1565. * megaraid_mbox_prepare_pthru - prepare a command for physical devices
  1566. * @adapter : pointer to controller's soft state
  1567. * @scb : scsi control block
  1568. * @scp : scsi command from the mid-layer
  1569. *
  1570. * Prepare a command for the scsi physical devices.
  1571. */
  1572. static void
  1573. megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
  1574. struct scsi_cmnd *scp)
  1575. {
  1576. mbox_ccb_t *ccb;
  1577. mraid_passthru_t *pthru;
  1578. uint8_t channel;
  1579. uint8_t target;
  1580. ccb = (mbox_ccb_t *)scb->ccb;
  1581. pthru = ccb->pthru;
  1582. channel = scb->dev_channel;
  1583. target = scb->dev_target;
  1584. // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
  1585. pthru->timeout = 4;
  1586. pthru->ars = 1;
  1587. pthru->islogical = 0;
  1588. pthru->channel = 0;
  1589. pthru->target = (channel << 4) | target;
  1590. pthru->logdrv = SCP2LUN(scp);
  1591. pthru->reqsenselen = 14;
  1592. pthru->cdblen = scp->cmd_len;
  1593. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  1594. if (scsi_bufflen(scp)) {
  1595. pthru->dataxferlen = scsi_bufflen(scp);
  1596. pthru->dataxferaddr = ccb->sgl_dma_h;
  1597. pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
  1598. }
  1599. else {
  1600. pthru->dataxferaddr = 0;
  1601. pthru->dataxferlen = 0;
  1602. pthru->numsge = 0;
  1603. }
  1604. return;
  1605. }
  1606. /**
  1607. * megaraid_mbox_prepare_epthru - prepare a command for physical devices
  1608. * @adapter : pointer to controller's soft state
  1609. * @scb : scsi control block
  1610. * @scp : scsi command from the mid-layer
  1611. *
  1612. * Prepare a command for the scsi physical devices. This routine prepares
  1613. * commands for devices which can take extended CDBs (>10 bytes).
  1614. */
  1615. static void
  1616. megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
  1617. struct scsi_cmnd *scp)
  1618. {
  1619. mbox_ccb_t *ccb;
  1620. mraid_epassthru_t *epthru;
  1621. uint8_t channel;
  1622. uint8_t target;
  1623. ccb = (mbox_ccb_t *)scb->ccb;
  1624. epthru = ccb->epthru;
  1625. channel = scb->dev_channel;
  1626. target = scb->dev_target;
  1627. // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
  1628. epthru->timeout = 4;
  1629. epthru->ars = 1;
  1630. epthru->islogical = 0;
  1631. epthru->channel = 0;
  1632. epthru->target = (channel << 4) | target;
  1633. epthru->logdrv = SCP2LUN(scp);
  1634. epthru->reqsenselen = 14;
  1635. epthru->cdblen = scp->cmd_len;
  1636. memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
  1637. if (scsi_bufflen(scp)) {
  1638. epthru->dataxferlen = scsi_bufflen(scp);
  1639. epthru->dataxferaddr = ccb->sgl_dma_h;
  1640. epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
  1641. }
  1642. else {
  1643. epthru->dataxferaddr = 0;
  1644. epthru->dataxferlen = 0;
  1645. epthru->numsge = 0;
  1646. }
  1647. return;
  1648. }
  1649. /**
  1650. * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
  1651. * @adapter : controller's soft state
  1652. *
  1653. * Interrupt acknowledgement sequence for memory mapped HBAs. Find out the
  1654. * completed command and put them on the completed list for later processing.
  1655. *
  1656. * Returns: 1 if the interrupt is valid, 0 otherwise
  1657. */
  1658. static int
  1659. megaraid_ack_sequence(adapter_t *adapter)
  1660. {
  1661. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  1662. mbox_t *mbox;
  1663. scb_t *scb;
  1664. uint8_t nstatus;
  1665. uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
  1666. struct list_head clist;
  1667. int handled;
  1668. uint32_t dword;
  1669. unsigned long flags;
  1670. int i, j;
  1671. mbox = raid_dev->mbox;
  1672. // move the SCBs from the firmware completed array to our local list
  1673. INIT_LIST_HEAD(&clist);
  1674. // loop till F/W has more commands for us to complete
  1675. handled = 0;
  1676. spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
  1677. do {
  1678. /*
  1679. * Check if a valid interrupt is pending. If found, force the
  1680. * interrupt line low.
  1681. */
  1682. dword = RDOUTDOOR(raid_dev);
  1683. if (dword != 0x10001234) break;
  1684. handled = 1;
  1685. WROUTDOOR(raid_dev, 0x10001234);
  1686. nstatus = 0;
  1687. // wait for valid numstatus to post
  1688. for (i = 0; i < 0xFFFFF; i++) {
  1689. if (mbox->numstatus != 0xFF) {
  1690. nstatus = mbox->numstatus;
  1691. break;
  1692. }
  1693. rmb();
  1694. }
  1695. mbox->numstatus = 0xFF;
  1696. adapter->outstanding_cmds -= nstatus;
  1697. for (i = 0; i < nstatus; i++) {
  1698. // wait for valid command index to post
  1699. for (j = 0; j < 0xFFFFF; j++) {
  1700. if (mbox->completed[i] != 0xFF) break;
  1701. rmb();
  1702. }
  1703. completed[i] = mbox->completed[i];
  1704. mbox->completed[i] = 0xFF;
  1705. if (completed[i] == 0xFF) {
  1706. con_log(CL_ANN, (KERN_CRIT
  1707. "megaraid: command posting timed out\n"));
  1708. BUG();
  1709. continue;
  1710. }
  1711. // Get SCB associated with this command id
  1712. if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
  1713. // a cmm command
  1714. scb = adapter->uscb_list + (completed[i] -
  1715. MBOX_MAX_SCSI_CMDS);
  1716. }
  1717. else {
  1718. // an os command
  1719. scb = adapter->kscb_list + completed[i];
  1720. }
  1721. scb->status = mbox->status;
  1722. list_add_tail(&scb->list, &clist);
  1723. }
  1724. // Acknowledge interrupt
  1725. WRINDOOR(raid_dev, 0x02);
  1726. } while(1);
  1727. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1728. // put the completed commands in the completed list. DPC would
  1729. // complete these commands later
  1730. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  1731. list_splice(&clist, &adapter->completed_list);
  1732. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  1733. // schedule the DPC if there is some work for it
  1734. if (handled)
  1735. tasklet_schedule(&adapter->dpc_h);
  1736. return handled;
  1737. }
  1738. /**
  1739. * megaraid_isr - isr for memory based mailbox based controllers
  1740. * @irq : irq
  1741. * @devp : pointer to our soft state
  1742. *
  1743. * Interrupt service routine for memory-mapped mailbox controllers.
  1744. */
  1745. static irqreturn_t
  1746. megaraid_isr(int irq, void *devp)
  1747. {
  1748. adapter_t *adapter = devp;
  1749. int handled;
  1750. handled = megaraid_ack_sequence(adapter);
  1751. /* Loop through any pending requests */
  1752. if (!adapter->quiescent) {
  1753. megaraid_mbox_runpendq(adapter, NULL);
  1754. }
  1755. return IRQ_RETVAL(handled);
  1756. }
  1757. /**
  1758. * megaraid_mbox_sync_scb - sync kernel buffers
  1759. * @adapter : controller's soft state
  1760. * @scb : pointer to the resource packet
  1761. *
  1762. * DMA sync if required.
  1763. */
  1764. static void
  1765. megaraid_mbox_sync_scb(adapter_t *adapter, scb_t *scb)
  1766. {
  1767. mbox_ccb_t *ccb;
  1768. ccb = (mbox_ccb_t *)scb->ccb;
  1769. if (scb->dma_direction == PCI_DMA_FROMDEVICE)
  1770. pci_dma_sync_sg_for_cpu(adapter->pdev,
  1771. scsi_sglist(scb->scp),
  1772. scsi_sg_count(scb->scp),
  1773. PCI_DMA_FROMDEVICE);
  1774. scsi_dma_unmap(scb->scp);
  1775. return;
  1776. }
  1777. /**
  1778. * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
  1779. * @devp : pointer to HBA soft state
  1780. *
  1781. * Pick up the commands from the completed list and send back to the owners.
  1782. * This is a reentrant function and does not assume any locks are held while
  1783. * it is being called.
  1784. */
  1785. static void
  1786. megaraid_mbox_dpc(unsigned long devp)
  1787. {
  1788. adapter_t *adapter = (adapter_t *)devp;
  1789. mraid_device_t *raid_dev;
  1790. struct list_head clist;
  1791. struct scatterlist *sgl;
  1792. scb_t *scb;
  1793. scb_t *tmp;
  1794. struct scsi_cmnd *scp;
  1795. mraid_passthru_t *pthru;
  1796. mraid_epassthru_t *epthru;
  1797. mbox_ccb_t *ccb;
  1798. int islogical;
  1799. int pdev_index;
  1800. int pdev_state;
  1801. mbox_t *mbox;
  1802. unsigned long flags;
  1803. uint8_t c;
  1804. int status;
  1805. uioc_t *kioc;
  1806. if (!adapter) return;
  1807. raid_dev = ADAP2RAIDDEV(adapter);
  1808. // move the SCBs from the completed list to our local list
  1809. INIT_LIST_HEAD(&clist);
  1810. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  1811. list_splice_init(&adapter->completed_list, &clist);
  1812. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  1813. list_for_each_entry_safe(scb, tmp, &clist, list) {
  1814. status = scb->status;
  1815. scp = scb->scp;
  1816. ccb = (mbox_ccb_t *)scb->ccb;
  1817. pthru = ccb->pthru;
  1818. epthru = ccb->epthru;
  1819. mbox = ccb->mbox;
  1820. // Make sure f/w has completed a valid command
  1821. if (scb->state != SCB_ISSUED) {
  1822. con_log(CL_ANN, (KERN_CRIT
  1823. "megaraid critical err: invalid command %d:%d:%p\n",
  1824. scb->sno, scb->state, scp));
  1825. BUG();
  1826. continue; // Must never happen!
  1827. }
  1828. // check for the management command and complete it right away
  1829. if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
  1830. scb->state = SCB_FREE;
  1831. scb->status = status;
  1832. // remove from local clist
  1833. list_del_init(&scb->list);
  1834. kioc = (uioc_t *)scb->gp;
  1835. kioc->status = 0;
  1836. megaraid_mbox_mm_done(adapter, scb);
  1837. continue;
  1838. }
  1839. // Was an abort issued for this command earlier
  1840. if (scb->state & SCB_ABORT) {
  1841. con_log(CL_ANN, (KERN_NOTICE
  1842. "megaraid: aborted cmd [%x] completed\n",
  1843. scb->sno));
  1844. }
  1845. /*
  1846. * If the inquiry came of a disk drive which is not part of
  1847. * any RAID array, expose it to the kernel. For this to be
  1848. * enabled, user must set the "megaraid_expose_unconf_disks"
  1849. * flag to 1 by specifying it on module parameter list.
  1850. * This would enable data migration off drives from other
  1851. * configurations.
  1852. */
  1853. islogical = MRAID_IS_LOGICAL(adapter, scp);
  1854. if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
  1855. && IS_RAID_CH(raid_dev, scb->dev_channel)) {
  1856. sgl = scsi_sglist(scp);
  1857. if (sg_page(sgl)) {
  1858. c = *(unsigned char *) sg_virt(&sgl[0]);
  1859. } else {
  1860. con_log(CL_ANN, (KERN_WARNING
  1861. "megaraid mailbox: invalid sg:%d\n",
  1862. __LINE__));
  1863. c = 0;
  1864. }
  1865. if ((c & 0x1F ) == TYPE_DISK) {
  1866. pdev_index = (scb->dev_channel * 16) +
  1867. scb->dev_target;
  1868. pdev_state =
  1869. raid_dev->pdrv_state[pdev_index] & 0x0F;
  1870. if (pdev_state == PDRV_ONLINE ||
  1871. pdev_state == PDRV_FAILED ||
  1872. pdev_state == PDRV_RBLD ||
  1873. pdev_state == PDRV_HOTSPARE ||
  1874. megaraid_expose_unconf_disks == 0) {
  1875. status = 0xF0;
  1876. }
  1877. }
  1878. }
  1879. // Convert MegaRAID status to Linux error code
  1880. switch (status) {
  1881. case 0x00:
  1882. scp->result = (DID_OK << 16);
  1883. break;
  1884. case 0x02:
  1885. /* set sense_buffer and result fields */
  1886. if (mbox->cmd == MBOXCMD_PASSTHRU ||
  1887. mbox->cmd == MBOXCMD_PASSTHRU64) {
  1888. memcpy(scp->sense_buffer, pthru->reqsensearea,
  1889. 14);
  1890. scp->result = DRIVER_SENSE << 24 |
  1891. DID_OK << 16 | CHECK_CONDITION << 1;
  1892. }
  1893. else {
  1894. if (mbox->cmd == MBOXCMD_EXTPTHRU) {
  1895. memcpy(scp->sense_buffer,
  1896. epthru->reqsensearea, 14);
  1897. scp->result = DRIVER_SENSE << 24 |
  1898. DID_OK << 16 |
  1899. CHECK_CONDITION << 1;
  1900. } else {
  1901. scp->sense_buffer[0] = 0x70;
  1902. scp->sense_buffer[2] = ABORTED_COMMAND;
  1903. scp->result = CHECK_CONDITION << 1;
  1904. }
  1905. }
  1906. break;
  1907. case 0x08:
  1908. scp->result = DID_BUS_BUSY << 16 | status;
  1909. break;
  1910. default:
  1911. /*
  1912. * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
  1913. * failed
  1914. */
  1915. if (scp->cmnd[0] == TEST_UNIT_READY) {
  1916. scp->result = DID_ERROR << 16 |
  1917. RESERVATION_CONFLICT << 1;
  1918. }
  1919. else
  1920. /*
  1921. * Error code returned is 1 if Reserve or Release
  1922. * failed or the input parameter is invalid
  1923. */
  1924. if (status == 1 && (scp->cmnd[0] == RESERVE ||
  1925. scp->cmnd[0] == RELEASE)) {
  1926. scp->result = DID_ERROR << 16 |
  1927. RESERVATION_CONFLICT << 1;
  1928. }
  1929. else {
  1930. scp->result = DID_BAD_TARGET << 16 | status;
  1931. }
  1932. }
  1933. // print a debug message for all failed commands
  1934. if (status) {
  1935. megaraid_mbox_display_scb(adapter, scb);
  1936. }
  1937. // Free our internal resources and call the mid-layer callback
  1938. // routine
  1939. megaraid_mbox_sync_scb(adapter, scb);
  1940. // remove from local clist
  1941. list_del_init(&scb->list);
  1942. // put back in free list
  1943. megaraid_dealloc_scb(adapter, scb);
  1944. // send the scsi packet back to kernel
  1945. scp->scsi_done(scp);
  1946. }
  1947. return;
  1948. }
  1949. /**
  1950. * megaraid_abort_handler - abort the scsi command
  1951. * @scp : command to be aborted
  1952. *
  1953. * Abort a previous SCSI request. Only commands on the pending list can be
  1954. * aborted. All the commands issued to the F/W must complete.
  1955. **/
  1956. static int
  1957. megaraid_abort_handler(struct scsi_cmnd *scp)
  1958. {
  1959. adapter_t *adapter;
  1960. mraid_device_t *raid_dev;
  1961. scb_t *scb;
  1962. scb_t *tmp;
  1963. int found;
  1964. unsigned long flags;
  1965. int i;
  1966. adapter = SCP2ADAPTER(scp);
  1967. raid_dev = ADAP2RAIDDEV(adapter);
  1968. con_log(CL_ANN, (KERN_WARNING
  1969. "megaraid: aborting cmd=%x <c=%d t=%d l=%d>\n",
  1970. scp->cmnd[0], SCP2CHANNEL(scp),
  1971. SCP2TARGET(scp), SCP2LUN(scp)));
  1972. // If FW has stopped responding, simply return failure
  1973. if (raid_dev->hw_error) {
  1974. con_log(CL_ANN, (KERN_NOTICE
  1975. "megaraid: hw error, not aborting\n"));
  1976. return FAILED;
  1977. }
  1978. // There might a race here, where the command was completed by the
  1979. // firmware and now it is on the completed list. Before we could
  1980. // complete the command to the kernel in dpc, the abort came.
  1981. // Find out if this is the case to avoid the race.
  1982. scb = NULL;
  1983. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  1984. list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
  1985. if (scb->scp == scp) { // Found command
  1986. list_del_init(&scb->list); // from completed list
  1987. con_log(CL_ANN, (KERN_WARNING
  1988. "megaraid: %d[%d:%d], abort from completed list\n",
  1989. scb->sno, scb->dev_channel, scb->dev_target));
  1990. scp->result = (DID_ABORT << 16);
  1991. scp->scsi_done(scp);
  1992. megaraid_dealloc_scb(adapter, scb);
  1993. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
  1994. flags);
  1995. return SUCCESS;
  1996. }
  1997. }
  1998. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  1999. // Find out if this command is still on the pending list. If it is and
  2000. // was never issued, abort and return success. If the command is owned
  2001. // by the firmware, we must wait for it to complete by the FW.
  2002. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  2003. list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
  2004. if (scb->scp == scp) { // Found command
  2005. list_del_init(&scb->list); // from pending list
  2006. ASSERT(!(scb->state & SCB_ISSUED));
  2007. con_log(CL_ANN, (KERN_WARNING
  2008. "megaraid abort: [%d:%d], driver owner\n",
  2009. scb->dev_channel, scb->dev_target));
  2010. scp->result = (DID_ABORT << 16);
  2011. scp->scsi_done(scp);
  2012. megaraid_dealloc_scb(adapter, scb);
  2013. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
  2014. flags);
  2015. return SUCCESS;
  2016. }
  2017. }
  2018. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  2019. // Check do we even own this command, in which case this would be
  2020. // owned by the firmware. The only way to locate the FW scb is to
  2021. // traverse through the list of all SCB, since driver does not
  2022. // maintain these SCBs on any list
  2023. found = 0;
  2024. spin_lock_irq(&adapter->lock);
  2025. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  2026. scb = adapter->kscb_list + i;
  2027. if (scb->scp == scp) {
  2028. found = 1;
  2029. if (!(scb->state & SCB_ISSUED)) {
  2030. con_log(CL_ANN, (KERN_WARNING
  2031. "megaraid abort: %d[%d:%d], invalid state\n",
  2032. scb->sno, scb->dev_channel, scb->dev_target));
  2033. BUG();
  2034. }
  2035. else {
  2036. con_log(CL_ANN, (KERN_WARNING
  2037. "megaraid abort: %d[%d:%d], fw owner\n",
  2038. scb->sno, scb->dev_channel, scb->dev_target));
  2039. }
  2040. }
  2041. }
  2042. spin_unlock_irq(&adapter->lock);
  2043. if (!found) {
  2044. con_log(CL_ANN, (KERN_WARNING "megaraid abort: do now own\n"));
  2045. // FIXME: Should there be a callback for this command?
  2046. return SUCCESS;
  2047. }
  2048. // We cannot actually abort a command owned by firmware, return
  2049. // failure and wait for reset. In host reset handler, we will find out
  2050. // if the HBA is still live
  2051. return FAILED;
  2052. }
  2053. /**
  2054. * megaraid_reset_handler - device reset handler for mailbox based driver
  2055. * @scp : reference command
  2056. *
  2057. * Reset handler for the mailbox based controller. First try to find out if
  2058. * the FW is still live, in which case the outstanding commands counter mut go
  2059. * down to 0. If that happens, also issue the reservation reset command to
  2060. * relinquish (possible) reservations on the logical drives connected to this
  2061. * host.
  2062. **/
  2063. static int
  2064. megaraid_reset_handler(struct scsi_cmnd *scp)
  2065. {
  2066. adapter_t *adapter;
  2067. scb_t *scb;
  2068. scb_t *tmp;
  2069. mraid_device_t *raid_dev;
  2070. unsigned long flags;
  2071. uint8_t raw_mbox[sizeof(mbox_t)];
  2072. int rval;
  2073. int recovery_window;
  2074. int recovering;
  2075. int i;
  2076. uioc_t *kioc;
  2077. adapter = SCP2ADAPTER(scp);
  2078. raid_dev = ADAP2RAIDDEV(adapter);
  2079. // return failure if adapter is not responding
  2080. if (raid_dev->hw_error) {
  2081. con_log(CL_ANN, (KERN_NOTICE
  2082. "megaraid: hw error, cannot reset\n"));
  2083. return FAILED;
  2084. }
  2085. // Under exceptional conditions, FW can take up to 3 minutes to
  2086. // complete command processing. Wait for additional 2 minutes for the
  2087. // pending commands counter to go down to 0. If it doesn't, let the
  2088. // controller be marked offline
  2089. // Also, reset all the commands currently owned by the driver
  2090. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  2091. list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
  2092. list_del_init(&scb->list); // from pending list
  2093. if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
  2094. con_log(CL_ANN, (KERN_WARNING
  2095. "megaraid: IOCTL packet with %d[%d:%d] being reset\n",
  2096. scb->sno, scb->dev_channel, scb->dev_target));
  2097. scb->status = -1;
  2098. kioc = (uioc_t *)scb->gp;
  2099. kioc->status = -EFAULT;
  2100. megaraid_mbox_mm_done(adapter, scb);
  2101. } else {
  2102. if (scb->scp == scp) { // Found command
  2103. con_log(CL_ANN, (KERN_WARNING
  2104. "megaraid: %d[%d:%d], reset from pending list\n",
  2105. scb->sno, scb->dev_channel, scb->dev_target));
  2106. } else {
  2107. con_log(CL_ANN, (KERN_WARNING
  2108. "megaraid: IO packet with %d[%d:%d] being reset\n",
  2109. scb->sno, scb->dev_channel, scb->dev_target));
  2110. }
  2111. scb->scp->result = (DID_RESET << 16);
  2112. scb->scp->scsi_done(scb->scp);
  2113. megaraid_dealloc_scb(adapter, scb);
  2114. }
  2115. }
  2116. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  2117. if (adapter->outstanding_cmds) {
  2118. con_log(CL_ANN, (KERN_NOTICE
  2119. "megaraid: %d outstanding commands. Max wait %d sec\n",
  2120. adapter->outstanding_cmds,
  2121. (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT)));
  2122. }
  2123. recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
  2124. recovering = adapter->outstanding_cmds;
  2125. for (i = 0; i < recovery_window; i++) {
  2126. megaraid_ack_sequence(adapter);
  2127. // print a message once every 5 seconds only
  2128. if (!(i % 5)) {
  2129. con_log(CL_ANN, (
  2130. "megaraid mbox: Wait for %d commands to complete:%d\n",
  2131. adapter->outstanding_cmds,
  2132. (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT) - i));
  2133. }
  2134. // bailout if no recovery happened in reset time
  2135. if (adapter->outstanding_cmds == 0) {
  2136. break;
  2137. }
  2138. msleep(1000);
  2139. }
  2140. spin_lock(&adapter->lock);
  2141. // If still outstanding commands, bail out
  2142. if (adapter->outstanding_cmds) {
  2143. con_log(CL_ANN, (KERN_WARNING
  2144. "megaraid mbox: critical hardware error!\n"));
  2145. raid_dev->hw_error = 1;
  2146. rval = FAILED;
  2147. goto out;
  2148. }
  2149. else {
  2150. con_log(CL_ANN, (KERN_NOTICE
  2151. "megaraid mbox: reset sequence completed successfully\n"));
  2152. }
  2153. // If the controller supports clustering, reset reservations
  2154. if (!adapter->ha) {
  2155. rval = SUCCESS;
  2156. goto out;
  2157. }
  2158. // clear reservations if any
  2159. raw_mbox[0] = CLUSTER_CMD;
  2160. raw_mbox[2] = RESET_RESERVATIONS;
  2161. rval = SUCCESS;
  2162. if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
  2163. con_log(CL_ANN,
  2164. (KERN_INFO "megaraid: reservation reset\n"));
  2165. }
  2166. else {
  2167. rval = FAILED;
  2168. con_log(CL_ANN, (KERN_WARNING
  2169. "megaraid: reservation reset failed\n"));
  2170. }
  2171. out:
  2172. spin_unlock(&adapter->lock);
  2173. return rval;
  2174. }
  2175. /*
  2176. * START: internal commands library
  2177. *
  2178. * This section of the driver has the common routine used by the driver and
  2179. * also has all the FW routines
  2180. */
  2181. /**
  2182. * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
  2183. * @adapter : controller's soft state
  2184. * @raw_mbox : the mailbox
  2185. *
  2186. * Issue a scb in synchronous and non-interrupt mode for mailbox based
  2187. * controllers.
  2188. */
  2189. static int
  2190. mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
  2191. {
  2192. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2193. mbox64_t *mbox64;
  2194. mbox_t *mbox;
  2195. uint8_t status;
  2196. int i;
  2197. mbox64 = raid_dev->mbox64;
  2198. mbox = raid_dev->mbox;
  2199. /*
  2200. * Wait until mailbox is free
  2201. */
  2202. if (megaraid_busywait_mbox(raid_dev) != 0)
  2203. goto blocked_mailbox;
  2204. /*
  2205. * Copy mailbox data into host structure
  2206. */
  2207. memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
  2208. mbox->cmdid = 0xFE;
  2209. mbox->busy = 1;
  2210. mbox->poll = 0;
  2211. mbox->ack = 0;
  2212. mbox->numstatus = 0xFF;
  2213. mbox->status = 0xFF;
  2214. wmb();
  2215. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  2216. // wait for maximum 1 second for status to post. If the status is not
  2217. // available within 1 second, assume FW is initializing and wait
  2218. // for an extended amount of time
  2219. if (mbox->numstatus == 0xFF) { // status not yet available
  2220. udelay(25);
  2221. for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
  2222. rmb();
  2223. msleep(1);
  2224. }
  2225. if (i == 1000) {
  2226. con_log(CL_ANN, (KERN_NOTICE
  2227. "megaraid mailbox: wait for FW to boot "));
  2228. for (i = 0; (mbox->numstatus == 0xFF) &&
  2229. (i < MBOX_RESET_WAIT); i++) {
  2230. rmb();
  2231. con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
  2232. MBOX_RESET_WAIT - i));
  2233. msleep(1000);
  2234. }
  2235. if (i == MBOX_RESET_WAIT) {
  2236. con_log(CL_ANN, (
  2237. "\nmegaraid mailbox: status not available\n"));
  2238. return -1;
  2239. }
  2240. con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
  2241. }
  2242. }
  2243. // wait for maximum 1 second for poll semaphore
  2244. if (mbox->poll != 0x77) {
  2245. udelay(25);
  2246. for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
  2247. rmb();
  2248. msleep(1);
  2249. }
  2250. if (i == 1000) {
  2251. con_log(CL_ANN, (KERN_WARNING
  2252. "megaraid mailbox: could not get poll semaphore\n"));
  2253. return -1;
  2254. }
  2255. }
  2256. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
  2257. wmb();
  2258. // wait for maximum 1 second for acknowledgement
  2259. if (RDINDOOR(raid_dev) & 0x2) {
  2260. udelay(25);
  2261. for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
  2262. rmb();
  2263. msleep(1);
  2264. }
  2265. if (i == 1000) {
  2266. con_log(CL_ANN, (KERN_WARNING
  2267. "megaraid mailbox: could not acknowledge\n"));
  2268. return -1;
  2269. }
  2270. }
  2271. mbox->poll = 0;
  2272. mbox->ack = 0x77;
  2273. status = mbox->status;
  2274. // invalidate the completed command id array. After command
  2275. // completion, firmware would write the valid id.
  2276. mbox->numstatus = 0xFF;
  2277. mbox->status = 0xFF;
  2278. for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
  2279. mbox->completed[i] = 0xFF;
  2280. }
  2281. return status;
  2282. blocked_mailbox:
  2283. con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
  2284. return -1;
  2285. }
  2286. /**
  2287. * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
  2288. * @adapter : controller's soft state
  2289. * @raw_mbox : the mailbox
  2290. *
  2291. * Issue a scb in synchronous and non-interrupt mode for mailbox based
  2292. * controllers. This is a faster version of the synchronous command and
  2293. * therefore can be called in interrupt-context as well.
  2294. */
  2295. static int
  2296. mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
  2297. {
  2298. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2299. mbox_t *mbox;
  2300. long i;
  2301. mbox = raid_dev->mbox;
  2302. // return immediately if the mailbox is busy
  2303. if (mbox->busy) return -1;
  2304. // Copy mailbox data into host structure
  2305. memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
  2306. mbox->cmdid = 0xFE;
  2307. mbox->busy = 1;
  2308. mbox->poll = 0;
  2309. mbox->ack = 0;
  2310. mbox->numstatus = 0xFF;
  2311. mbox->status = 0xFF;
  2312. wmb();
  2313. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  2314. for (i = 0; i < MBOX_SYNC_WAIT_CNT; i++) {
  2315. if (mbox->numstatus != 0xFF) break;
  2316. rmb();
  2317. udelay(MBOX_SYNC_DELAY_200);
  2318. }
  2319. if (i == MBOX_SYNC_WAIT_CNT) {
  2320. // We may need to re-calibrate the counter
  2321. con_log(CL_ANN, (KERN_CRIT
  2322. "megaraid: fast sync command timed out\n"));
  2323. }
  2324. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
  2325. wmb();
  2326. return mbox->status;
  2327. }
  2328. /**
  2329. * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
  2330. * @raid_dev : RAID device (HBA) soft state
  2331. *
  2332. * Wait until the controller's mailbox is available to accept more commands.
  2333. * Wait for at most 1 second.
  2334. */
  2335. static int
  2336. megaraid_busywait_mbox(mraid_device_t *raid_dev)
  2337. {
  2338. mbox_t *mbox = raid_dev->mbox;
  2339. int i = 0;
  2340. if (mbox->busy) {
  2341. udelay(25);
  2342. for (i = 0; mbox->busy && i < 1000; i++)
  2343. msleep(1);
  2344. }
  2345. if (i < 1000) return 0;
  2346. else return -1;
  2347. }
  2348. /**
  2349. * megaraid_mbox_product_info - some static information about the controller
  2350. * @adapter : our soft state
  2351. *
  2352. * Issue commands to the controller to grab some parameters required by our
  2353. * caller.
  2354. */
  2355. static int
  2356. megaraid_mbox_product_info(adapter_t *adapter)
  2357. {
  2358. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2359. mbox_t *mbox;
  2360. uint8_t raw_mbox[sizeof(mbox_t)];
  2361. mraid_pinfo_t *pinfo;
  2362. dma_addr_t pinfo_dma_h;
  2363. mraid_inquiry3_t *mraid_inq3;
  2364. int i;
  2365. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2366. mbox = (mbox_t *)raw_mbox;
  2367. /*
  2368. * Issue an ENQUIRY3 command to find out certain adapter parameters,
  2369. * e.g., max channels, max commands etc.
  2370. */
  2371. pinfo = pci_zalloc_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2372. &pinfo_dma_h);
  2373. if (pinfo == NULL) {
  2374. con_log(CL_ANN, (KERN_WARNING
  2375. "megaraid: out of memory, %s %d\n", __func__,
  2376. __LINE__));
  2377. return -1;
  2378. }
  2379. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2380. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2381. raw_mbox[0] = FC_NEW_CONFIG;
  2382. raw_mbox[2] = NC_SUBOP_ENQUIRY3;
  2383. raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
  2384. // Issue the command
  2385. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2386. con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
  2387. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2388. pinfo, pinfo_dma_h);
  2389. return -1;
  2390. }
  2391. /*
  2392. * Collect information about state of each physical drive
  2393. * attached to the controller. We will expose all the disks
  2394. * which are not part of RAID
  2395. */
  2396. mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
  2397. for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
  2398. raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
  2399. }
  2400. /*
  2401. * Get product info for information like number of channels,
  2402. * maximum commands supported.
  2403. */
  2404. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2405. mbox->xferaddr = (uint32_t)pinfo_dma_h;
  2406. raw_mbox[0] = FC_NEW_CONFIG;
  2407. raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
  2408. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2409. con_log(CL_ANN, (KERN_WARNING
  2410. "megaraid: product info failed\n"));
  2411. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2412. pinfo, pinfo_dma_h);
  2413. return -1;
  2414. }
  2415. /*
  2416. * Setup some parameters for host, as required by our caller
  2417. */
  2418. adapter->max_channel = pinfo->nchannels;
  2419. /*
  2420. * we will export all the logical drives on a single channel.
  2421. * Add 1 since inquires do not come for inititor ID
  2422. */
  2423. adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
  2424. adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
  2425. /*
  2426. * These are the maximum outstanding commands for the scsi-layer
  2427. */
  2428. adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
  2429. memset(adapter->fw_version, 0, VERSION_SIZE);
  2430. memset(adapter->bios_version, 0, VERSION_SIZE);
  2431. memcpy(adapter->fw_version, pinfo->fw_version, 4);
  2432. adapter->fw_version[4] = 0;
  2433. memcpy(adapter->bios_version, pinfo->bios_version, 4);
  2434. adapter->bios_version[4] = 0;
  2435. con_log(CL_ANN, (KERN_NOTICE
  2436. "megaraid: fw version:[%s] bios version:[%s]\n",
  2437. adapter->fw_version, adapter->bios_version));
  2438. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t), pinfo,
  2439. pinfo_dma_h);
  2440. return 0;
  2441. }
  2442. /**
  2443. * megaraid_mbox_extended_cdb - check for support for extended CDBs
  2444. * @adapter : soft state for the controller
  2445. *
  2446. * This routine check whether the controller in question supports extended
  2447. * ( > 10 bytes ) CDBs.
  2448. */
  2449. static int
  2450. megaraid_mbox_extended_cdb(adapter_t *adapter)
  2451. {
  2452. mbox_t *mbox;
  2453. uint8_t raw_mbox[sizeof(mbox_t)];
  2454. int rval;
  2455. mbox = (mbox_t *)raw_mbox;
  2456. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2457. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2458. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2459. raw_mbox[0] = MAIN_MISC_OPCODE;
  2460. raw_mbox[2] = SUPPORT_EXT_CDB;
  2461. /*
  2462. * Issue the command
  2463. */
  2464. rval = 0;
  2465. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2466. rval = -1;
  2467. }
  2468. return rval;
  2469. }
  2470. /**
  2471. * megaraid_mbox_support_ha - Do we support clustering
  2472. * @adapter : soft state for the controller
  2473. * @init_id : ID of the initiator
  2474. *
  2475. * Determine if the firmware supports clustering and the ID of the initiator.
  2476. */
  2477. static int
  2478. megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
  2479. {
  2480. mbox_t *mbox;
  2481. uint8_t raw_mbox[sizeof(mbox_t)];
  2482. int rval;
  2483. mbox = (mbox_t *)raw_mbox;
  2484. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2485. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2486. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2487. raw_mbox[0] = GET_TARGET_ID;
  2488. // Issue the command
  2489. *init_id = 7;
  2490. rval = -1;
  2491. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2492. *init_id = *(uint8_t *)adapter->ibuf;
  2493. con_log(CL_ANN, (KERN_INFO
  2494. "megaraid: cluster firmware, initiator ID: %d\n",
  2495. *init_id));
  2496. rval = 0;
  2497. }
  2498. return rval;
  2499. }
  2500. /**
  2501. * megaraid_mbox_support_random_del - Do we support random deletion
  2502. * @adapter : soft state for the controller
  2503. *
  2504. * Determine if the firmware supports random deletion.
  2505. * Return: 1 is operation supported, 0 otherwise
  2506. */
  2507. static int
  2508. megaraid_mbox_support_random_del(adapter_t *adapter)
  2509. {
  2510. mbox_t *mbox;
  2511. uint8_t raw_mbox[sizeof(mbox_t)];
  2512. int rval;
  2513. /*
  2514. * Newer firmware on Dell CERC expect a different
  2515. * random deletion handling, so disable it.
  2516. */
  2517. if (adapter->pdev->vendor == PCI_VENDOR_ID_AMI &&
  2518. adapter->pdev->device == PCI_DEVICE_ID_AMI_MEGARAID3 &&
  2519. adapter->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
  2520. adapter->pdev->subsystem_device == PCI_SUBSYS_ID_CERC_ATA100_4CH &&
  2521. (adapter->fw_version[0] > '6' ||
  2522. (adapter->fw_version[0] == '6' &&
  2523. adapter->fw_version[2] > '6') ||
  2524. (adapter->fw_version[0] == '6'
  2525. && adapter->fw_version[2] == '6'
  2526. && adapter->fw_version[3] > '1'))) {
  2527. con_log(CL_DLEVEL1, ("megaraid: disable random deletion\n"));
  2528. return 0;
  2529. }
  2530. mbox = (mbox_t *)raw_mbox;
  2531. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2532. raw_mbox[0] = FC_DEL_LOGDRV;
  2533. raw_mbox[2] = OP_SUP_DEL_LOGDRV;
  2534. // Issue the command
  2535. rval = 0;
  2536. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2537. con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
  2538. rval = 1;
  2539. }
  2540. return rval;
  2541. }
  2542. /**
  2543. * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
  2544. * @adapter : soft state for the controller
  2545. *
  2546. * Find out the maximum number of scatter-gather elements supported by the
  2547. * firmware.
  2548. */
  2549. static int
  2550. megaraid_mbox_get_max_sg(adapter_t *adapter)
  2551. {
  2552. mbox_t *mbox;
  2553. uint8_t raw_mbox[sizeof(mbox_t)];
  2554. int nsg;
  2555. mbox = (mbox_t *)raw_mbox;
  2556. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2557. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2558. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2559. raw_mbox[0] = MAIN_MISC_OPCODE;
  2560. raw_mbox[2] = GET_MAX_SG_SUPPORT;
  2561. // Issue the command
  2562. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2563. nsg = *(uint8_t *)adapter->ibuf;
  2564. }
  2565. else {
  2566. nsg = MBOX_DEFAULT_SG_SIZE;
  2567. }
  2568. if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
  2569. return nsg;
  2570. }
  2571. /**
  2572. * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
  2573. * @adapter : soft state for the controller
  2574. *
  2575. * Enumerate the RAID and SCSI channels for ROMB platforms so that channels
  2576. * can be exported as regular SCSI channels.
  2577. */
  2578. static void
  2579. megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
  2580. {
  2581. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2582. mbox_t *mbox;
  2583. uint8_t raw_mbox[sizeof(mbox_t)];
  2584. mbox = (mbox_t *)raw_mbox;
  2585. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2586. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2587. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2588. raw_mbox[0] = CHNL_CLASS;
  2589. raw_mbox[2] = GET_CHNL_CLASS;
  2590. // Issue the command. If the command fails, all channels are RAID
  2591. // channels
  2592. raid_dev->channel_class = 0xFF;
  2593. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2594. raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
  2595. }
  2596. return;
  2597. }
  2598. /**
  2599. * megaraid_mbox_flush_cache - flush adapter and disks cache
  2600. * @adapter : soft state for the controller
  2601. *
  2602. * Flush adapter cache followed by disks cache.
  2603. */
  2604. static void
  2605. megaraid_mbox_flush_cache(adapter_t *adapter)
  2606. {
  2607. mbox_t *mbox;
  2608. uint8_t raw_mbox[sizeof(mbox_t)];
  2609. mbox = (mbox_t *)raw_mbox;
  2610. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2611. raw_mbox[0] = FLUSH_ADAPTER;
  2612. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2613. con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
  2614. }
  2615. raw_mbox[0] = FLUSH_SYSTEM;
  2616. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2617. con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
  2618. }
  2619. return;
  2620. }
  2621. /**
  2622. * megaraid_mbox_fire_sync_cmd - fire the sync cmd
  2623. * @adapter : soft state for the controller
  2624. *
  2625. * Clears the pending cmds in FW and reinits its RAID structs.
  2626. */
  2627. static int
  2628. megaraid_mbox_fire_sync_cmd(adapter_t *adapter)
  2629. {
  2630. mbox_t *mbox;
  2631. uint8_t raw_mbox[sizeof(mbox_t)];
  2632. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2633. mbox64_t *mbox64;
  2634. int status = 0;
  2635. int i;
  2636. uint32_t dword;
  2637. mbox = (mbox_t *)raw_mbox;
  2638. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2639. raw_mbox[0] = 0xFF;
  2640. mbox64 = raid_dev->mbox64;
  2641. mbox = raid_dev->mbox;
  2642. /* Wait until mailbox is free */
  2643. if (megaraid_busywait_mbox(raid_dev) != 0) {
  2644. status = 1;
  2645. goto blocked_mailbox;
  2646. }
  2647. /* Copy mailbox data into host structure */
  2648. memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
  2649. mbox->cmdid = 0xFE;
  2650. mbox->busy = 1;
  2651. mbox->poll = 0;
  2652. mbox->ack = 0;
  2653. mbox->numstatus = 0;
  2654. mbox->status = 0;
  2655. wmb();
  2656. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  2657. /* Wait for maximum 1 min for status to post.
  2658. * If the Firmware SUPPORTS the ABOVE COMMAND,
  2659. * mbox->cmd will be set to 0
  2660. * else
  2661. * the firmware will reject the command with
  2662. * mbox->numstatus set to 1
  2663. */
  2664. i = 0;
  2665. status = 0;
  2666. while (!mbox->numstatus && mbox->cmd == 0xFF) {
  2667. rmb();
  2668. msleep(1);
  2669. i++;
  2670. if (i > 1000 * 60) {
  2671. status = 1;
  2672. break;
  2673. }
  2674. }
  2675. if (mbox->numstatus == 1)
  2676. status = 1; /*cmd not supported*/
  2677. /* Check for interrupt line */
  2678. dword = RDOUTDOOR(raid_dev);
  2679. WROUTDOOR(raid_dev, dword);
  2680. WRINDOOR(raid_dev,2);
  2681. return status;
  2682. blocked_mailbox:
  2683. con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n"));
  2684. return status;
  2685. }
  2686. /**
  2687. * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
  2688. * @adapter : controller's soft state
  2689. * @scb : SCB to be displayed
  2690. * @level : debug level for console print
  2691. *
  2692. * Diplay information about the given SCB iff the current debug level is
  2693. * verbose.
  2694. */
  2695. static void
  2696. megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
  2697. {
  2698. mbox_ccb_t *ccb;
  2699. struct scsi_cmnd *scp;
  2700. mbox_t *mbox;
  2701. int level;
  2702. int i;
  2703. ccb = (mbox_ccb_t *)scb->ccb;
  2704. scp = scb->scp;
  2705. mbox = ccb->mbox;
  2706. level = CL_DLEVEL3;
  2707. con_log(level, (KERN_NOTICE
  2708. "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
  2709. mbox->cmd, scb->sno));
  2710. con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
  2711. mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
  2712. mbox->numsge));
  2713. if (!scp) return;
  2714. con_log(level, (KERN_NOTICE "scsi cmnd: "));
  2715. for (i = 0; i < scp->cmd_len; i++) {
  2716. con_log(level, ("%#2.02x ", scp->cmnd[i]));
  2717. }
  2718. con_log(level, ("\n"));
  2719. return;
  2720. }
  2721. /**
  2722. * megaraid_mbox_setup_device_map - manage device ids
  2723. * @adapter : Driver's soft state
  2724. *
  2725. * Manage the device ids to have an appropriate mapping between the kernel
  2726. * scsi addresses and megaraid scsi and logical drive addresses. We export
  2727. * scsi devices on their actual addresses, whereas the logical drives are
  2728. * exported on a virtual scsi channel.
  2729. */
  2730. static void
  2731. megaraid_mbox_setup_device_map(adapter_t *adapter)
  2732. {
  2733. uint8_t c;
  2734. uint8_t t;
  2735. /*
  2736. * First fill the values on the logical drive channel
  2737. */
  2738. for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
  2739. adapter->device_ids[adapter->max_channel][t] =
  2740. (t < adapter->init_id) ? t : t - 1;
  2741. adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
  2742. /*
  2743. * Fill the values on the physical devices channels
  2744. */
  2745. for (c = 0; c < adapter->max_channel; c++)
  2746. for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
  2747. adapter->device_ids[c][t] = (c << 8) | t;
  2748. }
  2749. /*
  2750. * END: internal commands library
  2751. */
  2752. /*
  2753. * START: Interface for the common management module
  2754. *
  2755. * This is the module, which interfaces with the common management module to
  2756. * provide support for ioctl and sysfs
  2757. */
  2758. /**
  2759. * megaraid_cmm_register - register with the management module
  2760. * @adapter : HBA soft state
  2761. *
  2762. * Register with the management module, which allows applications to issue
  2763. * ioctl calls to the drivers. This interface is used by the management module
  2764. * to setup sysfs support as well.
  2765. */
  2766. static int
  2767. megaraid_cmm_register(adapter_t *adapter)
  2768. {
  2769. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2770. mraid_mmadp_t adp;
  2771. scb_t *scb;
  2772. mbox_ccb_t *ccb;
  2773. int rval;
  2774. int i;
  2775. // Allocate memory for the base list of scb for management module.
  2776. adapter->uscb_list = kcalloc(MBOX_MAX_USER_CMDS, sizeof(scb_t), GFP_KERNEL);
  2777. if (adapter->uscb_list == NULL) {
  2778. con_log(CL_ANN, (KERN_WARNING
  2779. "megaraid: out of memory, %s %d\n", __func__,
  2780. __LINE__));
  2781. return -1;
  2782. }
  2783. // Initialize the synchronization parameters for resources for
  2784. // commands for management module
  2785. INIT_LIST_HEAD(&adapter->uscb_pool);
  2786. spin_lock_init(USER_FREE_LIST_LOCK(adapter));
  2787. // link all the packets. Note, CCB for commands, coming from the
  2788. // commom management module, mailbox physical address are already
  2789. // setup by it. We just need placeholder for that in our local command
  2790. // control blocks
  2791. for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
  2792. scb = adapter->uscb_list + i;
  2793. ccb = raid_dev->uccb_list + i;
  2794. scb->ccb = (caddr_t)ccb;
  2795. ccb->mbox64 = raid_dev->umbox64 + i;
  2796. ccb->mbox = &ccb->mbox64->mbox32;
  2797. ccb->raw_mbox = (uint8_t *)ccb->mbox;
  2798. scb->gp = 0;
  2799. // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
  2800. // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
  2801. scb->sno = i + MBOX_MAX_SCSI_CMDS;
  2802. scb->scp = NULL;
  2803. scb->state = SCB_FREE;
  2804. scb->dma_direction = PCI_DMA_NONE;
  2805. scb->dma_type = MRAID_DMA_NONE;
  2806. scb->dev_channel = -1;
  2807. scb->dev_target = -1;
  2808. // put scb in the free pool
  2809. list_add_tail(&scb->list, &adapter->uscb_pool);
  2810. }
  2811. adp.unique_id = adapter->unique_id;
  2812. adp.drvr_type = DRVRTYPE_MBOX;
  2813. adp.drvr_data = (unsigned long)adapter;
  2814. adp.pdev = adapter->pdev;
  2815. adp.issue_uioc = megaraid_mbox_mm_handler;
  2816. adp.timeout = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
  2817. adp.max_kioc = MBOX_MAX_USER_CMDS;
  2818. if ((rval = mraid_mm_register_adp(&adp)) != 0) {
  2819. con_log(CL_ANN, (KERN_WARNING
  2820. "megaraid mbox: did not register with CMM\n"));
  2821. kfree(adapter->uscb_list);
  2822. }
  2823. return rval;
  2824. }
  2825. /**
  2826. * megaraid_cmm_unregister - un-register with the management module
  2827. * @adapter : HBA soft state
  2828. *
  2829. * Un-register with the management module.
  2830. * FIXME: mgmt module must return failure for unregister if it has pending
  2831. * commands in LLD.
  2832. */
  2833. static int
  2834. megaraid_cmm_unregister(adapter_t *adapter)
  2835. {
  2836. kfree(adapter->uscb_list);
  2837. mraid_mm_unregister_adp(adapter->unique_id);
  2838. return 0;
  2839. }
  2840. /**
  2841. * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
  2842. * @drvr_data : LLD specific data
  2843. * @kioc : CMM interface packet
  2844. * @action : command action
  2845. *
  2846. * This routine is invoked whenever the Common Management Module (CMM) has a
  2847. * command for us. The 'action' parameter specifies if this is a new command
  2848. * or otherwise.
  2849. */
  2850. static int
  2851. megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
  2852. {
  2853. adapter_t *adapter;
  2854. if (action != IOCTL_ISSUE) {
  2855. con_log(CL_ANN, (KERN_WARNING
  2856. "megaraid: unsupported management action:%#2x\n",
  2857. action));
  2858. return (-ENOTSUPP);
  2859. }
  2860. adapter = (adapter_t *)drvr_data;
  2861. // make sure this adapter is not being detached right now.
  2862. if (atomic_read(&adapter->being_detached)) {
  2863. con_log(CL_ANN, (KERN_WARNING
  2864. "megaraid: reject management request, detaching\n"));
  2865. return (-ENODEV);
  2866. }
  2867. switch (kioc->opcode) {
  2868. case GET_ADAP_INFO:
  2869. kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
  2870. (unsigned long)kioc->buf_vaddr);
  2871. kioc->done(kioc);
  2872. return kioc->status;
  2873. case MBOX_CMD:
  2874. return megaraid_mbox_mm_command(adapter, kioc);
  2875. default:
  2876. kioc->status = (-EINVAL);
  2877. kioc->done(kioc);
  2878. return (-EINVAL);
  2879. }
  2880. return 0; // not reached
  2881. }
  2882. /**
  2883. * megaraid_mbox_mm_command - issues commands routed through CMM
  2884. * @adapter : HBA soft state
  2885. * @kioc : management command packet
  2886. *
  2887. * Issues commands, which are routed through the management module.
  2888. */
  2889. static int
  2890. megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
  2891. {
  2892. struct list_head *head = &adapter->uscb_pool;
  2893. mbox64_t *mbox64;
  2894. uint8_t *raw_mbox;
  2895. scb_t *scb;
  2896. mbox_ccb_t *ccb;
  2897. unsigned long flags;
  2898. // detach one scb from free pool
  2899. spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
  2900. if (list_empty(head)) { // should never happen because of CMM
  2901. con_log(CL_ANN, (KERN_WARNING
  2902. "megaraid mbox: bug in cmm handler, lost resources\n"));
  2903. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  2904. return (-EINVAL);
  2905. }
  2906. scb = list_entry(head->next, scb_t, list);
  2907. list_del_init(&scb->list);
  2908. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  2909. scb->state = SCB_ACTIVE;
  2910. scb->dma_type = MRAID_DMA_NONE;
  2911. scb->dma_direction = PCI_DMA_NONE;
  2912. ccb = (mbox_ccb_t *)scb->ccb;
  2913. mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
  2914. raw_mbox = (uint8_t *)&mbox64->mbox32;
  2915. memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
  2916. scb->gp = (unsigned long)kioc;
  2917. /*
  2918. * If it is a logdrv random delete operation, we have to wait till
  2919. * there are no outstanding cmds at the fw and then issue it directly
  2920. */
  2921. if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
  2922. if (wait_till_fw_empty(adapter)) {
  2923. con_log(CL_ANN, (KERN_NOTICE
  2924. "megaraid mbox: LD delete, timed out\n"));
  2925. kioc->status = -ETIME;
  2926. scb->status = -1;
  2927. megaraid_mbox_mm_done(adapter, scb);
  2928. return (-ETIME);
  2929. }
  2930. INIT_LIST_HEAD(&scb->list);
  2931. scb->state = SCB_ISSUED;
  2932. if (mbox_post_cmd(adapter, scb) != 0) {
  2933. con_log(CL_ANN, (KERN_NOTICE
  2934. "megaraid mbox: LD delete, mailbox busy\n"));
  2935. kioc->status = -EBUSY;
  2936. scb->status = -1;
  2937. megaraid_mbox_mm_done(adapter, scb);
  2938. return (-EBUSY);
  2939. }
  2940. return 0;
  2941. }
  2942. // put the command on the pending list and execute
  2943. megaraid_mbox_runpendq(adapter, scb);
  2944. return 0;
  2945. }
  2946. static int
  2947. wait_till_fw_empty(adapter_t *adapter)
  2948. {
  2949. unsigned long flags = 0;
  2950. int i;
  2951. /*
  2952. * Set the quiescent flag to stop issuing cmds to FW.
  2953. */
  2954. spin_lock_irqsave(&adapter->lock, flags);
  2955. adapter->quiescent++;
  2956. spin_unlock_irqrestore(&adapter->lock, flags);
  2957. /*
  2958. * Wait till there are no more cmds outstanding at FW. Try for at most
  2959. * 60 seconds
  2960. */
  2961. for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
  2962. con_log(CL_DLEVEL1, (KERN_INFO
  2963. "megaraid: FW has %d pending commands\n",
  2964. adapter->outstanding_cmds));
  2965. msleep(1000);
  2966. }
  2967. return adapter->outstanding_cmds;
  2968. }
  2969. /**
  2970. * megaraid_mbox_mm_done - callback for CMM commands
  2971. * @adapter : HBA soft state
  2972. * @scb : completed command
  2973. *
  2974. * Callback routine for internal commands originated from the management
  2975. * module.
  2976. */
  2977. static void
  2978. megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
  2979. {
  2980. uioc_t *kioc;
  2981. mbox64_t *mbox64;
  2982. uint8_t *raw_mbox;
  2983. unsigned long flags;
  2984. kioc = (uioc_t *)scb->gp;
  2985. mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
  2986. mbox64->mbox32.status = scb->status;
  2987. raw_mbox = (uint8_t *)&mbox64->mbox32;
  2988. // put scb in the free pool
  2989. scb->state = SCB_FREE;
  2990. scb->scp = NULL;
  2991. spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
  2992. list_add(&scb->list, &adapter->uscb_pool);
  2993. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  2994. // if a delete logical drive operation succeeded, restart the
  2995. // controller
  2996. if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
  2997. adapter->quiescent--;
  2998. megaraid_mbox_runpendq(adapter, NULL);
  2999. }
  3000. kioc->done(kioc);
  3001. return;
  3002. }
  3003. /**
  3004. * gather_hbainfo - HBA characteristics for the applications
  3005. * @adapter : HBA soft state
  3006. * @hinfo : pointer to the caller's host info strucuture
  3007. */
  3008. static int
  3009. gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
  3010. {
  3011. uint8_t dmajor;
  3012. dmajor = megaraid_mbox_version[0];
  3013. hinfo->pci_vendor_id = adapter->pdev->vendor;
  3014. hinfo->pci_device_id = adapter->pdev->device;
  3015. hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
  3016. hinfo->subsys_device_id = adapter->pdev->subsystem_device;
  3017. hinfo->pci_bus = adapter->pdev->bus->number;
  3018. hinfo->pci_dev_fn = adapter->pdev->devfn;
  3019. hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
  3020. hinfo->irq = adapter->host->irq;
  3021. hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
  3022. hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
  3023. hinfo->host_no = adapter->host->host_no;
  3024. return 0;
  3025. }
  3026. /*
  3027. * END: Interface for the common management module
  3028. */
  3029. /**
  3030. * megaraid_sysfs_alloc_resources - allocate sysfs related resources
  3031. * @adapter : controller's soft state
  3032. *
  3033. * Allocate packets required to issue FW calls whenever the sysfs attributes
  3034. * are read. These attributes would require up-to-date information from the
  3035. * FW. Also set up resources for mutual exclusion to share these resources and
  3036. * the wait queue.
  3037. *
  3038. * Return 0 on success.
  3039. * Return -ERROR_CODE on failure.
  3040. */
  3041. static int
  3042. megaraid_sysfs_alloc_resources(adapter_t *adapter)
  3043. {
  3044. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3045. int rval = 0;
  3046. raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
  3047. raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
  3048. raid_dev->sysfs_buffer = pci_alloc_consistent(adapter->pdev,
  3049. PAGE_SIZE, &raid_dev->sysfs_buffer_dma);
  3050. if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
  3051. !raid_dev->sysfs_buffer) {
  3052. con_log(CL_ANN, (KERN_WARNING
  3053. "megaraid: out of memory, %s %d\n", __func__,
  3054. __LINE__));
  3055. rval = -ENOMEM;
  3056. megaraid_sysfs_free_resources(adapter);
  3057. }
  3058. mutex_init(&raid_dev->sysfs_mtx);
  3059. init_waitqueue_head(&raid_dev->sysfs_wait_q);
  3060. return rval;
  3061. }
  3062. /**
  3063. * megaraid_sysfs_free_resources - free sysfs related resources
  3064. * @adapter : controller's soft state
  3065. *
  3066. * Free packets allocated for sysfs FW commands
  3067. */
  3068. static void
  3069. megaraid_sysfs_free_resources(adapter_t *adapter)
  3070. {
  3071. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3072. kfree(raid_dev->sysfs_uioc);
  3073. kfree(raid_dev->sysfs_mbox64);
  3074. if (raid_dev->sysfs_buffer) {
  3075. pci_free_consistent(adapter->pdev, PAGE_SIZE,
  3076. raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
  3077. }
  3078. }
  3079. /**
  3080. * megaraid_sysfs_get_ldmap_done - callback for get ldmap
  3081. * @uioc : completed packet
  3082. *
  3083. * Callback routine called in the ISR/tasklet context for get ldmap call
  3084. */
  3085. static void
  3086. megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
  3087. {
  3088. adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
  3089. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3090. uioc->status = 0;
  3091. wake_up(&raid_dev->sysfs_wait_q);
  3092. }
  3093. /**
  3094. * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
  3095. * @data : timed out packet
  3096. *
  3097. * Timeout routine to recover and return to application, in case the adapter
  3098. * has stopped responding. A timeout of 60 seconds for this command seems like
  3099. * a good value.
  3100. */
  3101. static void
  3102. megaraid_sysfs_get_ldmap_timeout(unsigned long data)
  3103. {
  3104. uioc_t *uioc = (uioc_t *)data;
  3105. adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
  3106. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3107. uioc->status = -ETIME;
  3108. wake_up(&raid_dev->sysfs_wait_q);
  3109. }
  3110. /**
  3111. * megaraid_sysfs_get_ldmap - get update logical drive map
  3112. * @adapter : controller's soft state
  3113. *
  3114. * This routine will be called whenever user reads the logical drive
  3115. * attributes, go get the current logical drive mapping table from the
  3116. * firmware. We use the management API's to issue commands to the controller.
  3117. *
  3118. * NOTE: The commands issuance functionality is not generalized and
  3119. * implemented in context of "get ld map" command only. If required, the
  3120. * command issuance logical can be trivially pulled out and implemented as a
  3121. * standalone library. For now, this should suffice since there is no other
  3122. * user of this interface.
  3123. *
  3124. * Return 0 on success.
  3125. * Return -1 on failure.
  3126. */
  3127. static int
  3128. megaraid_sysfs_get_ldmap(adapter_t *adapter)
  3129. {
  3130. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3131. uioc_t *uioc;
  3132. mbox64_t *mbox64;
  3133. mbox_t *mbox;
  3134. char *raw_mbox;
  3135. struct timer_list sysfs_timer;
  3136. struct timer_list *timerp;
  3137. caddr_t ldmap;
  3138. int rval = 0;
  3139. /*
  3140. * Allow only one read at a time to go through the sysfs attributes
  3141. */
  3142. mutex_lock(&raid_dev->sysfs_mtx);
  3143. uioc = raid_dev->sysfs_uioc;
  3144. mbox64 = raid_dev->sysfs_mbox64;
  3145. ldmap = raid_dev->sysfs_buffer;
  3146. memset(uioc, 0, sizeof(uioc_t));
  3147. memset(mbox64, 0, sizeof(mbox64_t));
  3148. memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
  3149. mbox = &mbox64->mbox32;
  3150. raw_mbox = (char *)mbox;
  3151. uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
  3152. uioc->buf_vaddr = (caddr_t)adapter;
  3153. uioc->status = -ENODATA;
  3154. uioc->done = megaraid_sysfs_get_ldmap_done;
  3155. /*
  3156. * Prepare the mailbox packet to get the current logical drive mapping
  3157. * table
  3158. */
  3159. mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
  3160. raw_mbox[0] = FC_DEL_LOGDRV;
  3161. raw_mbox[2] = OP_GET_LDID_MAP;
  3162. /*
  3163. * Setup a timer to recover from a non-responding controller
  3164. */
  3165. timerp = &sysfs_timer;
  3166. init_timer(timerp);
  3167. timerp->function = megaraid_sysfs_get_ldmap_timeout;
  3168. timerp->data = (unsigned long)uioc;
  3169. timerp->expires = jiffies + 60 * HZ;
  3170. add_timer(timerp);
  3171. /*
  3172. * Send the command to the firmware
  3173. */
  3174. rval = megaraid_mbox_mm_command(adapter, uioc);
  3175. if (rval == 0) { // command successfully issued
  3176. wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
  3177. /*
  3178. * Check if the command timed out
  3179. */
  3180. if (uioc->status == -ETIME) {
  3181. con_log(CL_ANN, (KERN_NOTICE
  3182. "megaraid: sysfs get ld map timed out\n"));
  3183. rval = -ETIME;
  3184. }
  3185. else {
  3186. rval = mbox->status;
  3187. }
  3188. if (rval == 0) {
  3189. memcpy(raid_dev->curr_ldmap, ldmap,
  3190. sizeof(raid_dev->curr_ldmap));
  3191. }
  3192. else {
  3193. con_log(CL_ANN, (KERN_NOTICE
  3194. "megaraid: get ld map failed with %x\n", rval));
  3195. }
  3196. }
  3197. else {
  3198. con_log(CL_ANN, (KERN_NOTICE
  3199. "megaraid: could not issue ldmap command:%x\n", rval));
  3200. }
  3201. del_timer_sync(timerp);
  3202. mutex_unlock(&raid_dev->sysfs_mtx);
  3203. return rval;
  3204. }
  3205. /**
  3206. * megaraid_sysfs_show_app_hndl - display application handle for this adapter
  3207. * @cdev : class device object representation for the host
  3208. * @buf : buffer to send data to
  3209. *
  3210. * Display the handle used by the applications while executing management
  3211. * tasks on the adapter. We invoke a management module API to get the adapter
  3212. * handle, since we do not interface with applications directly.
  3213. */
  3214. static ssize_t
  3215. megaraid_sysfs_show_app_hndl(struct device *dev, struct device_attribute *attr,
  3216. char *buf)
  3217. {
  3218. struct Scsi_Host *shost = class_to_shost(dev);
  3219. adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
  3220. uint32_t app_hndl;
  3221. app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
  3222. return snprintf(buf, 8, "%u\n", app_hndl);
  3223. }
  3224. /**
  3225. * megaraid_sysfs_show_ldnum - display the logical drive number for this device
  3226. * @dev : device object representation for the scsi device
  3227. * @attr : device attribute to show
  3228. * @buf : buffer to send data to
  3229. *
  3230. * Display the logical drive number for the device in question, if it a valid
  3231. * logical drive. For physical devices, "-1" is returned.
  3232. *
  3233. * The logical drive number is displayed in following format:
  3234. *
  3235. * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
  3236. *
  3237. * <int> <int> <int> <int>
  3238. */
  3239. static ssize_t
  3240. megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf)
  3241. {
  3242. struct scsi_device *sdev = to_scsi_device(dev);
  3243. adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
  3244. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3245. int scsi_id = -1;
  3246. int logical_drv = -1;
  3247. int ldid_map = -1;
  3248. uint32_t app_hndl = 0;
  3249. int mapped_sdev_id;
  3250. int rval;
  3251. int i;
  3252. if (raid_dev->random_del_supported &&
  3253. MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
  3254. rval = megaraid_sysfs_get_ldmap(adapter);
  3255. if (rval == 0) {
  3256. for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
  3257. mapped_sdev_id = sdev->id;
  3258. if (sdev->id > adapter->init_id) {
  3259. mapped_sdev_id -= 1;
  3260. }
  3261. if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
  3262. scsi_id = sdev->id;
  3263. logical_drv = i;
  3264. ldid_map = raid_dev->curr_ldmap[i];
  3265. app_hndl = mraid_mm_adapter_app_handle(
  3266. adapter->unique_id);
  3267. break;
  3268. }
  3269. }
  3270. }
  3271. else {
  3272. con_log(CL_ANN, (KERN_NOTICE
  3273. "megaraid: sysfs get ld map failed: %x\n",
  3274. rval));
  3275. }
  3276. }
  3277. return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
  3278. ldid_map, app_hndl);
  3279. }
  3280. /*
  3281. * END: Mailbox Low Level Driver
  3282. */
  3283. module_init(megaraid_init);
  3284. module_exit(megaraid_exit);
  3285. /* vim: set ts=8 sw=8 tw=78 ai si: */