hpsa.c 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901
  1. /*
  2. * Disk Array driver for HP Smart Array SAS controllers
  3. * Copyright 2000, 2009 Hewlett-Packard Development Company, L.P.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; version 2 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  12. * NON INFRINGEMENT. See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * Questions/Comments/Bugfixes to iss_storagedev@hp.com
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/types.h>
  24. #include <linux/pci.h>
  25. #include <linux/kernel.h>
  26. #include <linux/slab.h>
  27. #include <linux/delay.h>
  28. #include <linux/fs.h>
  29. #include <linux/timer.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/init.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/compat.h>
  35. #include <linux/blktrace_api.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/io.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/completion.h>
  40. #include <linux/moduleparam.h>
  41. #include <scsi/scsi.h>
  42. #include <scsi/scsi_cmnd.h>
  43. #include <scsi/scsi_device.h>
  44. #include <scsi/scsi_host.h>
  45. #include <scsi/scsi_tcq.h>
  46. #include <linux/cciss_ioctl.h>
  47. #include <linux/string.h>
  48. #include <linux/bitmap.h>
  49. #include <asm/atomic.h>
  50. #include <linux/kthread.h>
  51. #include "hpsa_cmd.h"
  52. #include "hpsa.h"
  53. /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
  54. #define HPSA_DRIVER_VERSION "2.0.1-3"
  55. #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
  56. /* How long to wait (in milliseconds) for board to go into simple mode */
  57. #define MAX_CONFIG_WAIT 30000
  58. #define MAX_IOCTL_CONFIG_WAIT 1000
  59. /*define how many times we will try a command because of bus resets */
  60. #define MAX_CMD_RETRIES 3
  61. /* Embedded module documentation macros - see modules.h */
  62. MODULE_AUTHOR("Hewlett-Packard Company");
  63. MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
  64. HPSA_DRIVER_VERSION);
  65. MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
  66. MODULE_VERSION(HPSA_DRIVER_VERSION);
  67. MODULE_LICENSE("GPL");
  68. static int hpsa_allow_any;
  69. module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
  70. MODULE_PARM_DESC(hpsa_allow_any,
  71. "Allow hpsa driver to access unknown HP Smart Array hardware");
  72. /* define the PCI info for the cards we can control */
  73. static const struct pci_device_id hpsa_pci_device_id[] = {
  74. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
  75. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
  76. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
  77. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
  78. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
  79. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324a},
  80. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324b},
  81. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233},
  82. #define PCI_DEVICE_ID_HP_CISSF 0x333f
  83. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x333F},
  84. {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  85. PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
  86. {0,}
  87. };
  88. MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
  89. /* board_id = Subsystem Device ID & Vendor ID
  90. * product = Marketing Name for the board
  91. * access = Address of the struct of function pointers
  92. */
  93. static struct board_type products[] = {
  94. {0x3241103C, "Smart Array P212", &SA5_access},
  95. {0x3243103C, "Smart Array P410", &SA5_access},
  96. {0x3245103C, "Smart Array P410i", &SA5_access},
  97. {0x3247103C, "Smart Array P411", &SA5_access},
  98. {0x3249103C, "Smart Array P812", &SA5_access},
  99. {0x324a103C, "Smart Array P712m", &SA5_access},
  100. {0x324b103C, "Smart Array P711m", &SA5_access},
  101. {0x3233103C, "StorageWorks P1210m", &SA5_access},
  102. {0x333F103C, "StorageWorks P1210m", &SA5_access},
  103. {0xFFFF103C, "Unknown Smart Array", &SA5_access},
  104. };
  105. static int number_of_controllers;
  106. static irqreturn_t do_hpsa_intr(int irq, void *dev_id);
  107. static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg);
  108. static void start_io(struct ctlr_info *h);
  109. #ifdef CONFIG_COMPAT
  110. static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg);
  111. #endif
  112. static void cmd_free(struct ctlr_info *h, struct CommandList *c);
  113. static void cmd_special_free(struct ctlr_info *h, struct CommandList *c);
  114. static struct CommandList *cmd_alloc(struct ctlr_info *h);
  115. static struct CommandList *cmd_special_alloc(struct ctlr_info *h);
  116. static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
  117. void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
  118. int cmd_type);
  119. static int hpsa_scsi_queue_command(struct scsi_cmnd *cmd,
  120. void (*done)(struct scsi_cmnd *));
  121. static void hpsa_scan_start(struct Scsi_Host *);
  122. static int hpsa_scan_finished(struct Scsi_Host *sh,
  123. unsigned long elapsed_time);
  124. static int hpsa_change_queue_depth(struct scsi_device *sdev,
  125. int qdepth, int reason);
  126. static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
  127. static int hpsa_slave_alloc(struct scsi_device *sdev);
  128. static void hpsa_slave_destroy(struct scsi_device *sdev);
  129. static ssize_t raid_level_show(struct device *dev,
  130. struct device_attribute *attr, char *buf);
  131. static ssize_t lunid_show(struct device *dev,
  132. struct device_attribute *attr, char *buf);
  133. static ssize_t unique_id_show(struct device *dev,
  134. struct device_attribute *attr, char *buf);
  135. static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
  136. static ssize_t host_store_rescan(struct device *dev,
  137. struct device_attribute *attr, const char *buf, size_t count);
  138. static int check_for_unit_attention(struct ctlr_info *h,
  139. struct CommandList *c);
  140. static void check_ioctl_unit_attention(struct ctlr_info *h,
  141. struct CommandList *c);
  142. /* performant mode helper functions */
  143. static void calc_bucket_map(int *bucket, int num_buckets,
  144. int nsgs, int *bucket_map);
  145. static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
  146. static inline u32 next_command(struct ctlr_info *h);
  147. static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
  148. static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
  149. static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
  150. static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
  151. static struct device_attribute *hpsa_sdev_attrs[] = {
  152. &dev_attr_raid_level,
  153. &dev_attr_lunid,
  154. &dev_attr_unique_id,
  155. NULL,
  156. };
  157. static struct device_attribute *hpsa_shost_attrs[] = {
  158. &dev_attr_rescan,
  159. NULL,
  160. };
  161. static struct scsi_host_template hpsa_driver_template = {
  162. .module = THIS_MODULE,
  163. .name = "hpsa",
  164. .proc_name = "hpsa",
  165. .queuecommand = hpsa_scsi_queue_command,
  166. .scan_start = hpsa_scan_start,
  167. .scan_finished = hpsa_scan_finished,
  168. .change_queue_depth = hpsa_change_queue_depth,
  169. .this_id = -1,
  170. .sg_tablesize = MAXSGENTRIES,
  171. .use_clustering = ENABLE_CLUSTERING,
  172. .eh_device_reset_handler = hpsa_eh_device_reset_handler,
  173. .ioctl = hpsa_ioctl,
  174. .slave_alloc = hpsa_slave_alloc,
  175. .slave_destroy = hpsa_slave_destroy,
  176. #ifdef CONFIG_COMPAT
  177. .compat_ioctl = hpsa_compat_ioctl,
  178. #endif
  179. .sdev_attrs = hpsa_sdev_attrs,
  180. .shost_attrs = hpsa_shost_attrs,
  181. };
  182. static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
  183. {
  184. unsigned long *priv = shost_priv(sdev->host);
  185. return (struct ctlr_info *) *priv;
  186. }
  187. static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
  188. {
  189. unsigned long *priv = shost_priv(sh);
  190. return (struct ctlr_info *) *priv;
  191. }
  192. static struct task_struct *hpsa_scan_thread;
  193. static DEFINE_MUTEX(hpsa_scan_mutex);
  194. static LIST_HEAD(hpsa_scan_q);
  195. static int hpsa_scan_func(void *data);
  196. /**
  197. * add_to_scan_list() - add controller to rescan queue
  198. * @h: Pointer to the controller.
  199. *
  200. * Adds the controller to the rescan queue if not already on the queue.
  201. *
  202. * returns 1 if added to the queue, 0 if skipped (could be on the
  203. * queue already, or the controller could be initializing or shutting
  204. * down).
  205. **/
  206. static int add_to_scan_list(struct ctlr_info *h)
  207. {
  208. struct ctlr_info *test_h;
  209. int found = 0;
  210. int ret = 0;
  211. if (h->busy_initializing)
  212. return 0;
  213. /*
  214. * If we don't get the lock, it means the driver is unloading
  215. * and there's no point in scheduling a new scan.
  216. */
  217. if (!mutex_trylock(&h->busy_shutting_down))
  218. return 0;
  219. mutex_lock(&hpsa_scan_mutex);
  220. list_for_each_entry(test_h, &hpsa_scan_q, scan_list) {
  221. if (test_h == h) {
  222. found = 1;
  223. break;
  224. }
  225. }
  226. if (!found && !h->busy_scanning) {
  227. INIT_COMPLETION(h->scan_wait);
  228. list_add_tail(&h->scan_list, &hpsa_scan_q);
  229. ret = 1;
  230. }
  231. mutex_unlock(&hpsa_scan_mutex);
  232. mutex_unlock(&h->busy_shutting_down);
  233. return ret;
  234. }
  235. /**
  236. * remove_from_scan_list() - remove controller from rescan queue
  237. * @h: Pointer to the controller.
  238. *
  239. * Removes the controller from the rescan queue if present. Blocks if
  240. * the controller is currently conducting a rescan. The controller
  241. * can be in one of three states:
  242. * 1. Doesn't need a scan
  243. * 2. On the scan list, but not scanning yet (we remove it)
  244. * 3. Busy scanning (and not on the list). In this case we want to wait for
  245. * the scan to complete to make sure the scanning thread for this
  246. * controller is completely idle.
  247. **/
  248. static void remove_from_scan_list(struct ctlr_info *h)
  249. {
  250. struct ctlr_info *test_h, *tmp_h;
  251. mutex_lock(&hpsa_scan_mutex);
  252. list_for_each_entry_safe(test_h, tmp_h, &hpsa_scan_q, scan_list) {
  253. if (test_h == h) { /* state 2. */
  254. list_del(&h->scan_list);
  255. complete_all(&h->scan_wait);
  256. mutex_unlock(&hpsa_scan_mutex);
  257. return;
  258. }
  259. }
  260. if (h->busy_scanning) { /* state 3. */
  261. mutex_unlock(&hpsa_scan_mutex);
  262. wait_for_completion(&h->scan_wait);
  263. } else { /* state 1, nothing to do. */
  264. mutex_unlock(&hpsa_scan_mutex);
  265. }
  266. }
  267. /* hpsa_scan_func() - kernel thread used to rescan controllers
  268. * @data: Ignored.
  269. *
  270. * A kernel thread used scan for drive topology changes on
  271. * controllers. The thread processes only one controller at a time
  272. * using a queue. Controllers are added to the queue using
  273. * add_to_scan_list() and removed from the queue either after done
  274. * processing or using remove_from_scan_list().
  275. *
  276. * returns 0.
  277. **/
  278. static int hpsa_scan_func(__attribute__((unused)) void *data)
  279. {
  280. struct ctlr_info *h;
  281. int host_no;
  282. while (1) {
  283. set_current_state(TASK_INTERRUPTIBLE);
  284. schedule();
  285. if (kthread_should_stop())
  286. break;
  287. while (1) {
  288. mutex_lock(&hpsa_scan_mutex);
  289. if (list_empty(&hpsa_scan_q)) {
  290. mutex_unlock(&hpsa_scan_mutex);
  291. break;
  292. }
  293. h = list_entry(hpsa_scan_q.next, struct ctlr_info,
  294. scan_list);
  295. list_del(&h->scan_list);
  296. h->busy_scanning = 1;
  297. mutex_unlock(&hpsa_scan_mutex);
  298. host_no = h->scsi_host ? h->scsi_host->host_no : -1;
  299. hpsa_scan_start(h->scsi_host);
  300. complete_all(&h->scan_wait);
  301. mutex_lock(&hpsa_scan_mutex);
  302. h->busy_scanning = 0;
  303. mutex_unlock(&hpsa_scan_mutex);
  304. }
  305. }
  306. return 0;
  307. }
  308. static int check_for_unit_attention(struct ctlr_info *h,
  309. struct CommandList *c)
  310. {
  311. if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
  312. return 0;
  313. switch (c->err_info->SenseInfo[12]) {
  314. case STATE_CHANGED:
  315. dev_warn(&h->pdev->dev, "hpsa%d: a state change "
  316. "detected, command retried\n", h->ctlr);
  317. break;
  318. case LUN_FAILED:
  319. dev_warn(&h->pdev->dev, "hpsa%d: LUN failure "
  320. "detected, action required\n", h->ctlr);
  321. break;
  322. case REPORT_LUNS_CHANGED:
  323. dev_warn(&h->pdev->dev, "hpsa%d: report LUN data "
  324. "changed\n", h->ctlr);
  325. /*
  326. * Here, we could call add_to_scan_list and wake up the scan thread,
  327. * except that it's quite likely that we will get more than one
  328. * REPORT_LUNS_CHANGED condition in quick succession, which means
  329. * that those which occur after the first one will likely happen
  330. * *during* the hpsa_scan_thread's rescan. And the rescan code is not
  331. * robust enough to restart in the middle, undoing what it has already
  332. * done, and it's not clear that it's even possible to do this, since
  333. * part of what it does is notify the SCSI mid layer, which starts
  334. * doing it's own i/o to read partition tables and so on, and the
  335. * driver doesn't have visibility to know what might need undoing.
  336. * In any event, if possible, it is horribly complicated to get right
  337. * so we just don't do it for now.
  338. *
  339. * Note: this REPORT_LUNS_CHANGED condition only occurs on the MSA2012.
  340. */
  341. break;
  342. case POWER_OR_RESET:
  343. dev_warn(&h->pdev->dev, "hpsa%d: a power on "
  344. "or device reset detected\n", h->ctlr);
  345. break;
  346. case UNIT_ATTENTION_CLEARED:
  347. dev_warn(&h->pdev->dev, "hpsa%d: unit attention "
  348. "cleared by another initiator\n", h->ctlr);
  349. break;
  350. default:
  351. dev_warn(&h->pdev->dev, "hpsa%d: unknown "
  352. "unit attention detected\n", h->ctlr);
  353. break;
  354. }
  355. return 1;
  356. }
  357. static ssize_t host_store_rescan(struct device *dev,
  358. struct device_attribute *attr,
  359. const char *buf, size_t count)
  360. {
  361. struct ctlr_info *h;
  362. struct Scsi_Host *shost = class_to_shost(dev);
  363. h = shost_to_hba(shost);
  364. if (add_to_scan_list(h)) {
  365. wake_up_process(hpsa_scan_thread);
  366. wait_for_completion_interruptible(&h->scan_wait);
  367. }
  368. return count;
  369. }
  370. /* Enqueuing and dequeuing functions for cmdlists. */
  371. static inline void addQ(struct hlist_head *list, struct CommandList *c)
  372. {
  373. hlist_add_head(&c->list, list);
  374. }
  375. static inline u32 next_command(struct ctlr_info *h)
  376. {
  377. u32 a;
  378. if (unlikely(h->transMethod != CFGTBL_Trans_Performant))
  379. return h->access.command_completed(h);
  380. if ((*(h->reply_pool_head) & 1) == (h->reply_pool_wraparound)) {
  381. a = *(h->reply_pool_head); /* Next cmd in ring buffer */
  382. (h->reply_pool_head)++;
  383. h->commands_outstanding--;
  384. } else {
  385. a = FIFO_EMPTY;
  386. }
  387. /* Check for wraparound */
  388. if (h->reply_pool_head == (h->reply_pool + h->max_commands)) {
  389. h->reply_pool_head = h->reply_pool;
  390. h->reply_pool_wraparound ^= 1;
  391. }
  392. return a;
  393. }
  394. /* set_performant_mode: Modify the tag for cciss performant
  395. * set bit 0 for pull model, bits 3-1 for block fetch
  396. * register number
  397. */
  398. static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
  399. {
  400. if (likely(h->transMethod == CFGTBL_Trans_Performant))
  401. c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
  402. }
  403. static void enqueue_cmd_and_start_io(struct ctlr_info *h,
  404. struct CommandList *c)
  405. {
  406. unsigned long flags;
  407. set_performant_mode(h, c);
  408. spin_lock_irqsave(&h->lock, flags);
  409. addQ(&h->reqQ, c);
  410. h->Qdepth++;
  411. start_io(h);
  412. spin_unlock_irqrestore(&h->lock, flags);
  413. }
  414. static inline void removeQ(struct CommandList *c)
  415. {
  416. if (WARN_ON(hlist_unhashed(&c->list)))
  417. return;
  418. hlist_del_init(&c->list);
  419. }
  420. static inline int is_hba_lunid(unsigned char scsi3addr[])
  421. {
  422. return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
  423. }
  424. static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
  425. {
  426. return (scsi3addr[3] & 0xC0) == 0x40;
  427. }
  428. static inline int is_scsi_rev_5(struct ctlr_info *h)
  429. {
  430. if (!h->hba_inquiry_data)
  431. return 0;
  432. if ((h->hba_inquiry_data[2] & 0x07) == 5)
  433. return 1;
  434. return 0;
  435. }
  436. static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
  437. "UNKNOWN"
  438. };
  439. #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
  440. static ssize_t raid_level_show(struct device *dev,
  441. struct device_attribute *attr, char *buf)
  442. {
  443. ssize_t l = 0;
  444. unsigned char rlevel;
  445. struct ctlr_info *h;
  446. struct scsi_device *sdev;
  447. struct hpsa_scsi_dev_t *hdev;
  448. unsigned long flags;
  449. sdev = to_scsi_device(dev);
  450. h = sdev_to_hba(sdev);
  451. spin_lock_irqsave(&h->lock, flags);
  452. hdev = sdev->hostdata;
  453. if (!hdev) {
  454. spin_unlock_irqrestore(&h->lock, flags);
  455. return -ENODEV;
  456. }
  457. /* Is this even a logical drive? */
  458. if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
  459. spin_unlock_irqrestore(&h->lock, flags);
  460. l = snprintf(buf, PAGE_SIZE, "N/A\n");
  461. return l;
  462. }
  463. rlevel = hdev->raid_level;
  464. spin_unlock_irqrestore(&h->lock, flags);
  465. if (rlevel > RAID_UNKNOWN)
  466. rlevel = RAID_UNKNOWN;
  467. l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
  468. return l;
  469. }
  470. static ssize_t lunid_show(struct device *dev,
  471. struct device_attribute *attr, char *buf)
  472. {
  473. struct ctlr_info *h;
  474. struct scsi_device *sdev;
  475. struct hpsa_scsi_dev_t *hdev;
  476. unsigned long flags;
  477. unsigned char lunid[8];
  478. sdev = to_scsi_device(dev);
  479. h = sdev_to_hba(sdev);
  480. spin_lock_irqsave(&h->lock, flags);
  481. hdev = sdev->hostdata;
  482. if (!hdev) {
  483. spin_unlock_irqrestore(&h->lock, flags);
  484. return -ENODEV;
  485. }
  486. memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
  487. spin_unlock_irqrestore(&h->lock, flags);
  488. return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  489. lunid[0], lunid[1], lunid[2], lunid[3],
  490. lunid[4], lunid[5], lunid[6], lunid[7]);
  491. }
  492. static ssize_t unique_id_show(struct device *dev,
  493. struct device_attribute *attr, char *buf)
  494. {
  495. struct ctlr_info *h;
  496. struct scsi_device *sdev;
  497. struct hpsa_scsi_dev_t *hdev;
  498. unsigned long flags;
  499. unsigned char sn[16];
  500. sdev = to_scsi_device(dev);
  501. h = sdev_to_hba(sdev);
  502. spin_lock_irqsave(&h->lock, flags);
  503. hdev = sdev->hostdata;
  504. if (!hdev) {
  505. spin_unlock_irqrestore(&h->lock, flags);
  506. return -ENODEV;
  507. }
  508. memcpy(sn, hdev->device_id, sizeof(sn));
  509. spin_unlock_irqrestore(&h->lock, flags);
  510. return snprintf(buf, 16 * 2 + 2,
  511. "%02X%02X%02X%02X%02X%02X%02X%02X"
  512. "%02X%02X%02X%02X%02X%02X%02X%02X\n",
  513. sn[0], sn[1], sn[2], sn[3],
  514. sn[4], sn[5], sn[6], sn[7],
  515. sn[8], sn[9], sn[10], sn[11],
  516. sn[12], sn[13], sn[14], sn[15]);
  517. }
  518. static int hpsa_find_target_lun(struct ctlr_info *h,
  519. unsigned char scsi3addr[], int bus, int *target, int *lun)
  520. {
  521. /* finds an unused bus, target, lun for a new physical device
  522. * assumes h->devlock is held
  523. */
  524. int i, found = 0;
  525. DECLARE_BITMAP(lun_taken, HPSA_MAX_SCSI_DEVS_PER_HBA);
  526. memset(&lun_taken[0], 0, HPSA_MAX_SCSI_DEVS_PER_HBA >> 3);
  527. for (i = 0; i < h->ndevices; i++) {
  528. if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
  529. set_bit(h->dev[i]->target, lun_taken);
  530. }
  531. for (i = 0; i < HPSA_MAX_SCSI_DEVS_PER_HBA; i++) {
  532. if (!test_bit(i, lun_taken)) {
  533. /* *bus = 1; */
  534. *target = i;
  535. *lun = 0;
  536. found = 1;
  537. break;
  538. }
  539. }
  540. return !found;
  541. }
  542. /* Add an entry into h->dev[] array. */
  543. static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
  544. struct hpsa_scsi_dev_t *device,
  545. struct hpsa_scsi_dev_t *added[], int *nadded)
  546. {
  547. /* assumes h->devlock is held */
  548. int n = h->ndevices;
  549. int i;
  550. unsigned char addr1[8], addr2[8];
  551. struct hpsa_scsi_dev_t *sd;
  552. if (n >= HPSA_MAX_SCSI_DEVS_PER_HBA) {
  553. dev_err(&h->pdev->dev, "too many devices, some will be "
  554. "inaccessible.\n");
  555. return -1;
  556. }
  557. /* physical devices do not have lun or target assigned until now. */
  558. if (device->lun != -1)
  559. /* Logical device, lun is already assigned. */
  560. goto lun_assigned;
  561. /* If this device a non-zero lun of a multi-lun device
  562. * byte 4 of the 8-byte LUN addr will contain the logical
  563. * unit no, zero otherise.
  564. */
  565. if (device->scsi3addr[4] == 0) {
  566. /* This is not a non-zero lun of a multi-lun device */
  567. if (hpsa_find_target_lun(h, device->scsi3addr,
  568. device->bus, &device->target, &device->lun) != 0)
  569. return -1;
  570. goto lun_assigned;
  571. }
  572. /* This is a non-zero lun of a multi-lun device.
  573. * Search through our list and find the device which
  574. * has the same 8 byte LUN address, excepting byte 4.
  575. * Assign the same bus and target for this new LUN.
  576. * Use the logical unit number from the firmware.
  577. */
  578. memcpy(addr1, device->scsi3addr, 8);
  579. addr1[4] = 0;
  580. for (i = 0; i < n; i++) {
  581. sd = h->dev[i];
  582. memcpy(addr2, sd->scsi3addr, 8);
  583. addr2[4] = 0;
  584. /* differ only in byte 4? */
  585. if (memcmp(addr1, addr2, 8) == 0) {
  586. device->bus = sd->bus;
  587. device->target = sd->target;
  588. device->lun = device->scsi3addr[4];
  589. break;
  590. }
  591. }
  592. if (device->lun == -1) {
  593. dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
  594. " suspect firmware bug or unsupported hardware "
  595. "configuration.\n");
  596. return -1;
  597. }
  598. lun_assigned:
  599. h->dev[n] = device;
  600. h->ndevices++;
  601. added[*nadded] = device;
  602. (*nadded)++;
  603. /* initially, (before registering with scsi layer) we don't
  604. * know our hostno and we don't want to print anything first
  605. * time anyway (the scsi layer's inquiries will show that info)
  606. */
  607. /* if (hostno != -1) */
  608. dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
  609. scsi_device_type(device->devtype), hostno,
  610. device->bus, device->target, device->lun);
  611. return 0;
  612. }
  613. /* Replace an entry from h->dev[] array. */
  614. static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
  615. int entry, struct hpsa_scsi_dev_t *new_entry,
  616. struct hpsa_scsi_dev_t *added[], int *nadded,
  617. struct hpsa_scsi_dev_t *removed[], int *nremoved)
  618. {
  619. /* assumes h->devlock is held */
  620. BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA);
  621. removed[*nremoved] = h->dev[entry];
  622. (*nremoved)++;
  623. h->dev[entry] = new_entry;
  624. added[*nadded] = new_entry;
  625. (*nadded)++;
  626. dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
  627. scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
  628. new_entry->target, new_entry->lun);
  629. }
  630. /* Remove an entry from h->dev[] array. */
  631. static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
  632. struct hpsa_scsi_dev_t *removed[], int *nremoved)
  633. {
  634. /* assumes h->devlock is held */
  635. int i;
  636. struct hpsa_scsi_dev_t *sd;
  637. BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA);
  638. sd = h->dev[entry];
  639. removed[*nremoved] = h->dev[entry];
  640. (*nremoved)++;
  641. for (i = entry; i < h->ndevices-1; i++)
  642. h->dev[i] = h->dev[i+1];
  643. h->ndevices--;
  644. dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
  645. scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
  646. sd->lun);
  647. }
  648. #define SCSI3ADDR_EQ(a, b) ( \
  649. (a)[7] == (b)[7] && \
  650. (a)[6] == (b)[6] && \
  651. (a)[5] == (b)[5] && \
  652. (a)[4] == (b)[4] && \
  653. (a)[3] == (b)[3] && \
  654. (a)[2] == (b)[2] && \
  655. (a)[1] == (b)[1] && \
  656. (a)[0] == (b)[0])
  657. static void fixup_botched_add(struct ctlr_info *h,
  658. struct hpsa_scsi_dev_t *added)
  659. {
  660. /* called when scsi_add_device fails in order to re-adjust
  661. * h->dev[] to match the mid layer's view.
  662. */
  663. unsigned long flags;
  664. int i, j;
  665. spin_lock_irqsave(&h->lock, flags);
  666. for (i = 0; i < h->ndevices; i++) {
  667. if (h->dev[i] == added) {
  668. for (j = i; j < h->ndevices-1; j++)
  669. h->dev[j] = h->dev[j+1];
  670. h->ndevices--;
  671. break;
  672. }
  673. }
  674. spin_unlock_irqrestore(&h->lock, flags);
  675. kfree(added);
  676. }
  677. static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
  678. struct hpsa_scsi_dev_t *dev2)
  679. {
  680. if ((is_logical_dev_addr_mode(dev1->scsi3addr) ||
  681. (dev1->lun != -1 && dev2->lun != -1)) &&
  682. dev1->devtype != 0x0C)
  683. return (memcmp(dev1, dev2, sizeof(*dev1)) == 0);
  684. /* we compare everything except lun and target as these
  685. * are not yet assigned. Compare parts likely
  686. * to differ first
  687. */
  688. if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
  689. sizeof(dev1->scsi3addr)) != 0)
  690. return 0;
  691. if (memcmp(dev1->device_id, dev2->device_id,
  692. sizeof(dev1->device_id)) != 0)
  693. return 0;
  694. if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
  695. return 0;
  696. if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
  697. return 0;
  698. if (memcmp(dev1->revision, dev2->revision, sizeof(dev1->revision)) != 0)
  699. return 0;
  700. if (dev1->devtype != dev2->devtype)
  701. return 0;
  702. if (dev1->raid_level != dev2->raid_level)
  703. return 0;
  704. if (dev1->bus != dev2->bus)
  705. return 0;
  706. return 1;
  707. }
  708. /* Find needle in haystack. If exact match found, return DEVICE_SAME,
  709. * and return needle location in *index. If scsi3addr matches, but not
  710. * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
  711. * location in *index. If needle not found, return DEVICE_NOT_FOUND.
  712. */
  713. static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
  714. struct hpsa_scsi_dev_t *haystack[], int haystack_size,
  715. int *index)
  716. {
  717. int i;
  718. #define DEVICE_NOT_FOUND 0
  719. #define DEVICE_CHANGED 1
  720. #define DEVICE_SAME 2
  721. for (i = 0; i < haystack_size; i++) {
  722. if (haystack[i] == NULL) /* previously removed. */
  723. continue;
  724. if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
  725. *index = i;
  726. if (device_is_the_same(needle, haystack[i]))
  727. return DEVICE_SAME;
  728. else
  729. return DEVICE_CHANGED;
  730. }
  731. }
  732. *index = -1;
  733. return DEVICE_NOT_FOUND;
  734. }
  735. static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
  736. struct hpsa_scsi_dev_t *sd[], int nsds)
  737. {
  738. /* sd contains scsi3 addresses and devtypes, and inquiry
  739. * data. This function takes what's in sd to be the current
  740. * reality and updates h->dev[] to reflect that reality.
  741. */
  742. int i, entry, device_change, changes = 0;
  743. struct hpsa_scsi_dev_t *csd;
  744. unsigned long flags;
  745. struct hpsa_scsi_dev_t **added, **removed;
  746. int nadded, nremoved;
  747. struct Scsi_Host *sh = NULL;
  748. added = kzalloc(sizeof(*added) * HPSA_MAX_SCSI_DEVS_PER_HBA,
  749. GFP_KERNEL);
  750. removed = kzalloc(sizeof(*removed) * HPSA_MAX_SCSI_DEVS_PER_HBA,
  751. GFP_KERNEL);
  752. if (!added || !removed) {
  753. dev_warn(&h->pdev->dev, "out of memory in "
  754. "adjust_hpsa_scsi_table\n");
  755. goto free_and_out;
  756. }
  757. spin_lock_irqsave(&h->devlock, flags);
  758. /* find any devices in h->dev[] that are not in
  759. * sd[] and remove them from h->dev[], and for any
  760. * devices which have changed, remove the old device
  761. * info and add the new device info.
  762. */
  763. i = 0;
  764. nremoved = 0;
  765. nadded = 0;
  766. while (i < h->ndevices) {
  767. csd = h->dev[i];
  768. device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
  769. if (device_change == DEVICE_NOT_FOUND) {
  770. changes++;
  771. hpsa_scsi_remove_entry(h, hostno, i,
  772. removed, &nremoved);
  773. continue; /* remove ^^^, hence i not incremented */
  774. } else if (device_change == DEVICE_CHANGED) {
  775. changes++;
  776. hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
  777. added, &nadded, removed, &nremoved);
  778. /* Set it to NULL to prevent it from being freed
  779. * at the bottom of hpsa_update_scsi_devices()
  780. */
  781. sd[entry] = NULL;
  782. }
  783. i++;
  784. }
  785. /* Now, make sure every device listed in sd[] is also
  786. * listed in h->dev[], adding them if they aren't found
  787. */
  788. for (i = 0; i < nsds; i++) {
  789. if (!sd[i]) /* if already added above. */
  790. continue;
  791. device_change = hpsa_scsi_find_entry(sd[i], h->dev,
  792. h->ndevices, &entry);
  793. if (device_change == DEVICE_NOT_FOUND) {
  794. changes++;
  795. if (hpsa_scsi_add_entry(h, hostno, sd[i],
  796. added, &nadded) != 0)
  797. break;
  798. sd[i] = NULL; /* prevent from being freed later. */
  799. } else if (device_change == DEVICE_CHANGED) {
  800. /* should never happen... */
  801. changes++;
  802. dev_warn(&h->pdev->dev,
  803. "device unexpectedly changed.\n");
  804. /* but if it does happen, we just ignore that device */
  805. }
  806. }
  807. spin_unlock_irqrestore(&h->devlock, flags);
  808. /* Don't notify scsi mid layer of any changes the first time through
  809. * (or if there are no changes) scsi_scan_host will do it later the
  810. * first time through.
  811. */
  812. if (hostno == -1 || !changes)
  813. goto free_and_out;
  814. sh = h->scsi_host;
  815. /* Notify scsi mid layer of any removed devices */
  816. for (i = 0; i < nremoved; i++) {
  817. struct scsi_device *sdev =
  818. scsi_device_lookup(sh, removed[i]->bus,
  819. removed[i]->target, removed[i]->lun);
  820. if (sdev != NULL) {
  821. scsi_remove_device(sdev);
  822. scsi_device_put(sdev);
  823. } else {
  824. /* We don't expect to get here.
  825. * future cmds to this device will get selection
  826. * timeout as if the device was gone.
  827. */
  828. dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
  829. " for removal.", hostno, removed[i]->bus,
  830. removed[i]->target, removed[i]->lun);
  831. }
  832. kfree(removed[i]);
  833. removed[i] = NULL;
  834. }
  835. /* Notify scsi mid layer of any added devices */
  836. for (i = 0; i < nadded; i++) {
  837. if (scsi_add_device(sh, added[i]->bus,
  838. added[i]->target, added[i]->lun) == 0)
  839. continue;
  840. dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
  841. "device not added.\n", hostno, added[i]->bus,
  842. added[i]->target, added[i]->lun);
  843. /* now we have to remove it from h->dev,
  844. * since it didn't get added to scsi mid layer
  845. */
  846. fixup_botched_add(h, added[i]);
  847. }
  848. free_and_out:
  849. kfree(added);
  850. kfree(removed);
  851. }
  852. /*
  853. * Lookup bus/target/lun and retrun corresponding struct hpsa_scsi_dev_t *
  854. * Assume's h->devlock is held.
  855. */
  856. static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
  857. int bus, int target, int lun)
  858. {
  859. int i;
  860. struct hpsa_scsi_dev_t *sd;
  861. for (i = 0; i < h->ndevices; i++) {
  862. sd = h->dev[i];
  863. if (sd->bus == bus && sd->target == target && sd->lun == lun)
  864. return sd;
  865. }
  866. return NULL;
  867. }
  868. /* link sdev->hostdata to our per-device structure. */
  869. static int hpsa_slave_alloc(struct scsi_device *sdev)
  870. {
  871. struct hpsa_scsi_dev_t *sd;
  872. unsigned long flags;
  873. struct ctlr_info *h;
  874. h = sdev_to_hba(sdev);
  875. spin_lock_irqsave(&h->devlock, flags);
  876. sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
  877. sdev_id(sdev), sdev->lun);
  878. if (sd != NULL)
  879. sdev->hostdata = sd;
  880. spin_unlock_irqrestore(&h->devlock, flags);
  881. return 0;
  882. }
  883. static void hpsa_slave_destroy(struct scsi_device *sdev)
  884. {
  885. /* nothing to do. */
  886. }
  887. static void hpsa_scsi_setup(struct ctlr_info *h)
  888. {
  889. h->ndevices = 0;
  890. h->scsi_host = NULL;
  891. spin_lock_init(&h->devlock);
  892. }
  893. static void complete_scsi_command(struct CommandList *cp,
  894. int timeout, u32 tag)
  895. {
  896. struct scsi_cmnd *cmd;
  897. struct ctlr_info *h;
  898. struct ErrorInfo *ei;
  899. unsigned char sense_key;
  900. unsigned char asc; /* additional sense code */
  901. unsigned char ascq; /* additional sense code qualifier */
  902. ei = cp->err_info;
  903. cmd = (struct scsi_cmnd *) cp->scsi_cmd;
  904. h = cp->h;
  905. scsi_dma_unmap(cmd); /* undo the DMA mappings */
  906. cmd->result = (DID_OK << 16); /* host byte */
  907. cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
  908. cmd->result |= (ei->ScsiStatus << 1);
  909. /* copy the sense data whether we need to or not. */
  910. memcpy(cmd->sense_buffer, ei->SenseInfo,
  911. ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
  912. SCSI_SENSE_BUFFERSIZE :
  913. ei->SenseLen);
  914. scsi_set_resid(cmd, ei->ResidualCnt);
  915. if (ei->CommandStatus == 0) {
  916. cmd->scsi_done(cmd);
  917. cmd_free(h, cp);
  918. return;
  919. }
  920. /* an error has occurred */
  921. switch (ei->CommandStatus) {
  922. case CMD_TARGET_STATUS:
  923. if (ei->ScsiStatus) {
  924. /* Get sense key */
  925. sense_key = 0xf & ei->SenseInfo[2];
  926. /* Get additional sense code */
  927. asc = ei->SenseInfo[12];
  928. /* Get addition sense code qualifier */
  929. ascq = ei->SenseInfo[13];
  930. }
  931. if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
  932. if (check_for_unit_attention(h, cp)) {
  933. cmd->result = DID_SOFT_ERROR << 16;
  934. break;
  935. }
  936. if (sense_key == ILLEGAL_REQUEST) {
  937. /*
  938. * SCSI REPORT_LUNS is commonly unsupported on
  939. * Smart Array. Suppress noisy complaint.
  940. */
  941. if (cp->Request.CDB[0] == REPORT_LUNS)
  942. break;
  943. /* If ASC/ASCQ indicate Logical Unit
  944. * Not Supported condition,
  945. */
  946. if ((asc == 0x25) && (ascq == 0x0)) {
  947. dev_warn(&h->pdev->dev, "cp %p "
  948. "has check condition\n", cp);
  949. break;
  950. }
  951. }
  952. if (sense_key == NOT_READY) {
  953. /* If Sense is Not Ready, Logical Unit
  954. * Not ready, Manual Intervention
  955. * required
  956. */
  957. if ((asc == 0x04) && (ascq == 0x03)) {
  958. dev_warn(&h->pdev->dev, "cp %p "
  959. "has check condition: unit "
  960. "not ready, manual "
  961. "intervention required\n", cp);
  962. break;
  963. }
  964. }
  965. if (sense_key == ABORTED_COMMAND) {
  966. /* Aborted command is retryable */
  967. dev_warn(&h->pdev->dev, "cp %p "
  968. "has check condition: aborted command: "
  969. "ASC: 0x%x, ASCQ: 0x%x\n",
  970. cp, asc, ascq);
  971. cmd->result = DID_SOFT_ERROR << 16;
  972. break;
  973. }
  974. /* Must be some other type of check condition */
  975. dev_warn(&h->pdev->dev, "cp %p has check condition: "
  976. "unknown type: "
  977. "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
  978. "Returning result: 0x%x, "
  979. "cmd=[%02x %02x %02x %02x %02x "
  980. "%02x %02x %02x %02x %02x %02x "
  981. "%02x %02x %02x %02x %02x]\n",
  982. cp, sense_key, asc, ascq,
  983. cmd->result,
  984. cmd->cmnd[0], cmd->cmnd[1],
  985. cmd->cmnd[2], cmd->cmnd[3],
  986. cmd->cmnd[4], cmd->cmnd[5],
  987. cmd->cmnd[6], cmd->cmnd[7],
  988. cmd->cmnd[8], cmd->cmnd[9],
  989. cmd->cmnd[10], cmd->cmnd[11],
  990. cmd->cmnd[12], cmd->cmnd[13],
  991. cmd->cmnd[14], cmd->cmnd[15]);
  992. break;
  993. }
  994. /* Problem was not a check condition
  995. * Pass it up to the upper layers...
  996. */
  997. if (ei->ScsiStatus) {
  998. dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
  999. "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
  1000. "Returning result: 0x%x\n",
  1001. cp, ei->ScsiStatus,
  1002. sense_key, asc, ascq,
  1003. cmd->result);
  1004. } else { /* scsi status is zero??? How??? */
  1005. dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
  1006. "Returning no connection.\n", cp),
  1007. /* Ordinarily, this case should never happen,
  1008. * but there is a bug in some released firmware
  1009. * revisions that allows it to happen if, for
  1010. * example, a 4100 backplane loses power and
  1011. * the tape drive is in it. We assume that
  1012. * it's a fatal error of some kind because we
  1013. * can't show that it wasn't. We will make it
  1014. * look like selection timeout since that is
  1015. * the most common reason for this to occur,
  1016. * and it's severe enough.
  1017. */
  1018. cmd->result = DID_NO_CONNECT << 16;
  1019. }
  1020. break;
  1021. case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
  1022. break;
  1023. case CMD_DATA_OVERRUN:
  1024. dev_warn(&h->pdev->dev, "cp %p has"
  1025. " completed with data overrun "
  1026. "reported\n", cp);
  1027. break;
  1028. case CMD_INVALID: {
  1029. /* print_bytes(cp, sizeof(*cp), 1, 0);
  1030. print_cmd(cp); */
  1031. /* We get CMD_INVALID if you address a non-existent device
  1032. * instead of a selection timeout (no response). You will
  1033. * see this if you yank out a drive, then try to access it.
  1034. * This is kind of a shame because it means that any other
  1035. * CMD_INVALID (e.g. driver bug) will get interpreted as a
  1036. * missing target. */
  1037. cmd->result = DID_NO_CONNECT << 16;
  1038. }
  1039. break;
  1040. case CMD_PROTOCOL_ERR:
  1041. dev_warn(&h->pdev->dev, "cp %p has "
  1042. "protocol error \n", cp);
  1043. break;
  1044. case CMD_HARDWARE_ERR:
  1045. cmd->result = DID_ERROR << 16;
  1046. dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
  1047. break;
  1048. case CMD_CONNECTION_LOST:
  1049. cmd->result = DID_ERROR << 16;
  1050. dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
  1051. break;
  1052. case CMD_ABORTED:
  1053. cmd->result = DID_ABORT << 16;
  1054. dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
  1055. cp, ei->ScsiStatus);
  1056. break;
  1057. case CMD_ABORT_FAILED:
  1058. cmd->result = DID_ERROR << 16;
  1059. dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
  1060. break;
  1061. case CMD_UNSOLICITED_ABORT:
  1062. cmd->result = DID_RESET << 16;
  1063. dev_warn(&h->pdev->dev, "cp %p aborted do to an unsolicited "
  1064. "abort\n", cp);
  1065. break;
  1066. case CMD_TIMEOUT:
  1067. cmd->result = DID_TIME_OUT << 16;
  1068. dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
  1069. break;
  1070. default:
  1071. cmd->result = DID_ERROR << 16;
  1072. dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
  1073. cp, ei->CommandStatus);
  1074. }
  1075. cmd->scsi_done(cmd);
  1076. cmd_free(h, cp);
  1077. }
  1078. static int hpsa_scsi_detect(struct ctlr_info *h)
  1079. {
  1080. struct Scsi_Host *sh;
  1081. int error;
  1082. sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
  1083. if (sh == NULL)
  1084. goto fail;
  1085. sh->io_port = 0;
  1086. sh->n_io_port = 0;
  1087. sh->this_id = -1;
  1088. sh->max_channel = 3;
  1089. sh->max_cmd_len = MAX_COMMAND_SIZE;
  1090. sh->max_lun = HPSA_MAX_LUN;
  1091. sh->max_id = HPSA_MAX_LUN;
  1092. sh->can_queue = h->nr_cmds;
  1093. sh->cmd_per_lun = h->nr_cmds;
  1094. h->scsi_host = sh;
  1095. sh->hostdata[0] = (unsigned long) h;
  1096. sh->irq = h->intr[PERF_MODE_INT];
  1097. sh->unique_id = sh->irq;
  1098. error = scsi_add_host(sh, &h->pdev->dev);
  1099. if (error)
  1100. goto fail_host_put;
  1101. scsi_scan_host(sh);
  1102. return 0;
  1103. fail_host_put:
  1104. dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_add_host"
  1105. " failed for controller %d\n", h->ctlr);
  1106. scsi_host_put(sh);
  1107. return error;
  1108. fail:
  1109. dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_host_alloc"
  1110. " failed for controller %d\n", h->ctlr);
  1111. return -ENOMEM;
  1112. }
  1113. static void hpsa_pci_unmap(struct pci_dev *pdev,
  1114. struct CommandList *c, int sg_used, int data_direction)
  1115. {
  1116. int i;
  1117. union u64bit addr64;
  1118. for (i = 0; i < sg_used; i++) {
  1119. addr64.val32.lower = c->SG[i].Addr.lower;
  1120. addr64.val32.upper = c->SG[i].Addr.upper;
  1121. pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
  1122. data_direction);
  1123. }
  1124. }
  1125. static void hpsa_map_one(struct pci_dev *pdev,
  1126. struct CommandList *cp,
  1127. unsigned char *buf,
  1128. size_t buflen,
  1129. int data_direction)
  1130. {
  1131. u64 addr64;
  1132. if (buflen == 0 || data_direction == PCI_DMA_NONE) {
  1133. cp->Header.SGList = 0;
  1134. cp->Header.SGTotal = 0;
  1135. return;
  1136. }
  1137. addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
  1138. cp->SG[0].Addr.lower =
  1139. (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
  1140. cp->SG[0].Addr.upper =
  1141. (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
  1142. cp->SG[0].Len = buflen;
  1143. cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */
  1144. cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
  1145. }
  1146. static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
  1147. struct CommandList *c)
  1148. {
  1149. DECLARE_COMPLETION_ONSTACK(wait);
  1150. c->waiting = &wait;
  1151. enqueue_cmd_and_start_io(h, c);
  1152. wait_for_completion(&wait);
  1153. }
  1154. static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
  1155. struct CommandList *c, int data_direction)
  1156. {
  1157. int retry_count = 0;
  1158. do {
  1159. memset(c->err_info, 0, sizeof(c->err_info));
  1160. hpsa_scsi_do_simple_cmd_core(h, c);
  1161. retry_count++;
  1162. } while (check_for_unit_attention(h, c) && retry_count <= 3);
  1163. hpsa_pci_unmap(h->pdev, c, 1, data_direction);
  1164. }
  1165. static void hpsa_scsi_interpret_error(struct CommandList *cp)
  1166. {
  1167. struct ErrorInfo *ei;
  1168. struct device *d = &cp->h->pdev->dev;
  1169. ei = cp->err_info;
  1170. switch (ei->CommandStatus) {
  1171. case CMD_TARGET_STATUS:
  1172. dev_warn(d, "cmd %p has completed with errors\n", cp);
  1173. dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
  1174. ei->ScsiStatus);
  1175. if (ei->ScsiStatus == 0)
  1176. dev_warn(d, "SCSI status is abnormally zero. "
  1177. "(probably indicates selection timeout "
  1178. "reported incorrectly due to a known "
  1179. "firmware bug, circa July, 2001.)\n");
  1180. break;
  1181. case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
  1182. dev_info(d, "UNDERRUN\n");
  1183. break;
  1184. case CMD_DATA_OVERRUN:
  1185. dev_warn(d, "cp %p has completed with data overrun\n", cp);
  1186. break;
  1187. case CMD_INVALID: {
  1188. /* controller unfortunately reports SCSI passthru's
  1189. * to non-existent targets as invalid commands.
  1190. */
  1191. dev_warn(d, "cp %p is reported invalid (probably means "
  1192. "target device no longer present)\n", cp);
  1193. /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
  1194. print_cmd(cp); */
  1195. }
  1196. break;
  1197. case CMD_PROTOCOL_ERR:
  1198. dev_warn(d, "cp %p has protocol error \n", cp);
  1199. break;
  1200. case CMD_HARDWARE_ERR:
  1201. /* cmd->result = DID_ERROR << 16; */
  1202. dev_warn(d, "cp %p had hardware error\n", cp);
  1203. break;
  1204. case CMD_CONNECTION_LOST:
  1205. dev_warn(d, "cp %p had connection lost\n", cp);
  1206. break;
  1207. case CMD_ABORTED:
  1208. dev_warn(d, "cp %p was aborted\n", cp);
  1209. break;
  1210. case CMD_ABORT_FAILED:
  1211. dev_warn(d, "cp %p reports abort failed\n", cp);
  1212. break;
  1213. case CMD_UNSOLICITED_ABORT:
  1214. dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
  1215. break;
  1216. case CMD_TIMEOUT:
  1217. dev_warn(d, "cp %p timed out\n", cp);
  1218. break;
  1219. default:
  1220. dev_warn(d, "cp %p returned unknown status %x\n", cp,
  1221. ei->CommandStatus);
  1222. }
  1223. }
  1224. static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
  1225. unsigned char page, unsigned char *buf,
  1226. unsigned char bufsize)
  1227. {
  1228. int rc = IO_OK;
  1229. struct CommandList *c;
  1230. struct ErrorInfo *ei;
  1231. c = cmd_special_alloc(h);
  1232. if (c == NULL) { /* trouble... */
  1233. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  1234. return -ENOMEM;
  1235. }
  1236. fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize, page, scsi3addr, TYPE_CMD);
  1237. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
  1238. ei = c->err_info;
  1239. if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
  1240. hpsa_scsi_interpret_error(c);
  1241. rc = -1;
  1242. }
  1243. cmd_special_free(h, c);
  1244. return rc;
  1245. }
  1246. static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr)
  1247. {
  1248. int rc = IO_OK;
  1249. struct CommandList *c;
  1250. struct ErrorInfo *ei;
  1251. c = cmd_special_alloc(h);
  1252. if (c == NULL) { /* trouble... */
  1253. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  1254. return -1;
  1255. }
  1256. fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0, scsi3addr, TYPE_MSG);
  1257. hpsa_scsi_do_simple_cmd_core(h, c);
  1258. /* no unmap needed here because no data xfer. */
  1259. ei = c->err_info;
  1260. if (ei->CommandStatus != 0) {
  1261. hpsa_scsi_interpret_error(c);
  1262. rc = -1;
  1263. }
  1264. cmd_special_free(h, c);
  1265. return rc;
  1266. }
  1267. static void hpsa_get_raid_level(struct ctlr_info *h,
  1268. unsigned char *scsi3addr, unsigned char *raid_level)
  1269. {
  1270. int rc;
  1271. unsigned char *buf;
  1272. *raid_level = RAID_UNKNOWN;
  1273. buf = kzalloc(64, GFP_KERNEL);
  1274. if (!buf)
  1275. return;
  1276. rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64);
  1277. if (rc == 0)
  1278. *raid_level = buf[8];
  1279. if (*raid_level > RAID_UNKNOWN)
  1280. *raid_level = RAID_UNKNOWN;
  1281. kfree(buf);
  1282. return;
  1283. }
  1284. /* Get the device id from inquiry page 0x83 */
  1285. static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
  1286. unsigned char *device_id, int buflen)
  1287. {
  1288. int rc;
  1289. unsigned char *buf;
  1290. if (buflen > 16)
  1291. buflen = 16;
  1292. buf = kzalloc(64, GFP_KERNEL);
  1293. if (!buf)
  1294. return -1;
  1295. rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
  1296. if (rc == 0)
  1297. memcpy(device_id, &buf[8], buflen);
  1298. kfree(buf);
  1299. return rc != 0;
  1300. }
  1301. static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
  1302. struct ReportLUNdata *buf, int bufsize,
  1303. int extended_response)
  1304. {
  1305. int rc = IO_OK;
  1306. struct CommandList *c;
  1307. unsigned char scsi3addr[8];
  1308. struct ErrorInfo *ei;
  1309. c = cmd_special_alloc(h);
  1310. if (c == NULL) { /* trouble... */
  1311. dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  1312. return -1;
  1313. }
  1314. /* address the controller */
  1315. memset(scsi3addr, 0, sizeof(scsi3addr));
  1316. fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
  1317. buf, bufsize, 0, scsi3addr, TYPE_CMD);
  1318. if (extended_response)
  1319. c->Request.CDB[1] = extended_response;
  1320. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
  1321. ei = c->err_info;
  1322. if (ei->CommandStatus != 0 &&
  1323. ei->CommandStatus != CMD_DATA_UNDERRUN) {
  1324. hpsa_scsi_interpret_error(c);
  1325. rc = -1;
  1326. }
  1327. cmd_special_free(h, c);
  1328. return rc;
  1329. }
  1330. static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
  1331. struct ReportLUNdata *buf,
  1332. int bufsize, int extended_response)
  1333. {
  1334. return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
  1335. }
  1336. static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
  1337. struct ReportLUNdata *buf, int bufsize)
  1338. {
  1339. return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
  1340. }
  1341. static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
  1342. int bus, int target, int lun)
  1343. {
  1344. device->bus = bus;
  1345. device->target = target;
  1346. device->lun = lun;
  1347. }
  1348. static int hpsa_update_device_info(struct ctlr_info *h,
  1349. unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device)
  1350. {
  1351. #define OBDR_TAPE_INQ_SIZE 49
  1352. unsigned char *inq_buff;
  1353. inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
  1354. if (!inq_buff)
  1355. goto bail_out;
  1356. /* Do an inquiry to the device to see what it is. */
  1357. if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
  1358. (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
  1359. /* Inquiry failed (msg printed already) */
  1360. dev_err(&h->pdev->dev,
  1361. "hpsa_update_device_info: inquiry failed\n");
  1362. goto bail_out;
  1363. }
  1364. /* As a side effect, record the firmware version number
  1365. * if we happen to be talking to the RAID controller.
  1366. */
  1367. if (is_hba_lunid(scsi3addr))
  1368. memcpy(h->firm_ver, &inq_buff[32], 4);
  1369. this_device->devtype = (inq_buff[0] & 0x1f);
  1370. memcpy(this_device->scsi3addr, scsi3addr, 8);
  1371. memcpy(this_device->vendor, &inq_buff[8],
  1372. sizeof(this_device->vendor));
  1373. memcpy(this_device->model, &inq_buff[16],
  1374. sizeof(this_device->model));
  1375. memcpy(this_device->revision, &inq_buff[32],
  1376. sizeof(this_device->revision));
  1377. memset(this_device->device_id, 0,
  1378. sizeof(this_device->device_id));
  1379. hpsa_get_device_id(h, scsi3addr, this_device->device_id,
  1380. sizeof(this_device->device_id));
  1381. if (this_device->devtype == TYPE_DISK &&
  1382. is_logical_dev_addr_mode(scsi3addr))
  1383. hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
  1384. else
  1385. this_device->raid_level = RAID_UNKNOWN;
  1386. kfree(inq_buff);
  1387. return 0;
  1388. bail_out:
  1389. kfree(inq_buff);
  1390. return 1;
  1391. }
  1392. static unsigned char *msa2xxx_model[] = {
  1393. "MSA2012",
  1394. "MSA2024",
  1395. "MSA2312",
  1396. "MSA2324",
  1397. NULL,
  1398. };
  1399. static int is_msa2xxx(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
  1400. {
  1401. int i;
  1402. for (i = 0; msa2xxx_model[i]; i++)
  1403. if (strncmp(device->model, msa2xxx_model[i],
  1404. strlen(msa2xxx_model[i])) == 0)
  1405. return 1;
  1406. return 0;
  1407. }
  1408. /* Helper function to assign bus, target, lun mapping of devices.
  1409. * Puts non-msa2xxx logical volumes on bus 0, msa2xxx logical
  1410. * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
  1411. * Logical drive target and lun are assigned at this time, but
  1412. * physical device lun and target assignment are deferred (assigned
  1413. * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
  1414. */
  1415. static void figure_bus_target_lun(struct ctlr_info *h,
  1416. u8 *lunaddrbytes, int *bus, int *target, int *lun,
  1417. struct hpsa_scsi_dev_t *device)
  1418. {
  1419. u32 lunid;
  1420. if (is_logical_dev_addr_mode(lunaddrbytes)) {
  1421. /* logical device */
  1422. if (unlikely(is_scsi_rev_5(h))) {
  1423. /* p1210m, logical drives lun assignments
  1424. * match SCSI REPORT LUNS data.
  1425. */
  1426. lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
  1427. *bus = 0;
  1428. *target = 0;
  1429. *lun = (lunid & 0x3fff) + 1;
  1430. } else {
  1431. /* not p1210m... */
  1432. lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
  1433. if (is_msa2xxx(h, device)) {
  1434. /* msa2xxx way, put logicals on bus 1
  1435. * and match target/lun numbers box
  1436. * reports.
  1437. */
  1438. *bus = 1;
  1439. *target = (lunid >> 16) & 0x3fff;
  1440. *lun = lunid & 0x00ff;
  1441. } else {
  1442. /* Traditional smart array way. */
  1443. *bus = 0;
  1444. *lun = 0;
  1445. *target = lunid & 0x3fff;
  1446. }
  1447. }
  1448. } else {
  1449. /* physical device */
  1450. if (is_hba_lunid(lunaddrbytes))
  1451. if (unlikely(is_scsi_rev_5(h))) {
  1452. *bus = 0; /* put p1210m ctlr at 0,0,0 */
  1453. *target = 0;
  1454. *lun = 0;
  1455. return;
  1456. } else
  1457. *bus = 3; /* traditional smartarray */
  1458. else
  1459. *bus = 2; /* physical disk */
  1460. *target = -1;
  1461. *lun = -1; /* we will fill these in later. */
  1462. }
  1463. }
  1464. /*
  1465. * If there is no lun 0 on a target, linux won't find any devices.
  1466. * For the MSA2xxx boxes, we have to manually detect the enclosure
  1467. * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
  1468. * it for some reason. *tmpdevice is the target we're adding,
  1469. * this_device is a pointer into the current element of currentsd[]
  1470. * that we're building up in update_scsi_devices(), below.
  1471. * lunzerobits is a bitmap that tracks which targets already have a
  1472. * lun 0 assigned.
  1473. * Returns 1 if an enclosure was added, 0 if not.
  1474. */
  1475. static int add_msa2xxx_enclosure_device(struct ctlr_info *h,
  1476. struct hpsa_scsi_dev_t *tmpdevice,
  1477. struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
  1478. int bus, int target, int lun, unsigned long lunzerobits[],
  1479. int *nmsa2xxx_enclosures)
  1480. {
  1481. unsigned char scsi3addr[8];
  1482. if (test_bit(target, lunzerobits))
  1483. return 0; /* There is already a lun 0 on this target. */
  1484. if (!is_logical_dev_addr_mode(lunaddrbytes))
  1485. return 0; /* It's the logical targets that may lack lun 0. */
  1486. if (!is_msa2xxx(h, tmpdevice))
  1487. return 0; /* It's only the MSA2xxx that have this problem. */
  1488. if (lun == 0) /* if lun is 0, then obviously we have a lun 0. */
  1489. return 0;
  1490. if (is_hba_lunid(scsi3addr))
  1491. return 0; /* Don't add the RAID controller here. */
  1492. if (is_scsi_rev_5(h))
  1493. return 0; /* p1210m doesn't need to do this. */
  1494. #define MAX_MSA2XXX_ENCLOSURES 32
  1495. if (*nmsa2xxx_enclosures >= MAX_MSA2XXX_ENCLOSURES) {
  1496. dev_warn(&h->pdev->dev, "Maximum number of MSA2XXX "
  1497. "enclosures exceeded. Check your hardware "
  1498. "configuration.");
  1499. return 0;
  1500. }
  1501. memset(scsi3addr, 0, 8);
  1502. scsi3addr[3] = target;
  1503. if (hpsa_update_device_info(h, scsi3addr, this_device))
  1504. return 0;
  1505. (*nmsa2xxx_enclosures)++;
  1506. hpsa_set_bus_target_lun(this_device, bus, target, 0);
  1507. set_bit(target, lunzerobits);
  1508. return 1;
  1509. }
  1510. /*
  1511. * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
  1512. * logdev. The number of luns in physdev and logdev are returned in
  1513. * *nphysicals and *nlogicals, respectively.
  1514. * Returns 0 on success, -1 otherwise.
  1515. */
  1516. static int hpsa_gather_lun_info(struct ctlr_info *h,
  1517. int reportlunsize,
  1518. struct ReportLUNdata *physdev, u32 *nphysicals,
  1519. struct ReportLUNdata *logdev, u32 *nlogicals)
  1520. {
  1521. if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize, 0)) {
  1522. dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
  1523. return -1;
  1524. }
  1525. *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 8;
  1526. if (*nphysicals > HPSA_MAX_PHYS_LUN) {
  1527. dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
  1528. " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
  1529. *nphysicals - HPSA_MAX_PHYS_LUN);
  1530. *nphysicals = HPSA_MAX_PHYS_LUN;
  1531. }
  1532. if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
  1533. dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
  1534. return -1;
  1535. }
  1536. *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
  1537. /* Reject Logicals in excess of our max capability. */
  1538. if (*nlogicals > HPSA_MAX_LUN) {
  1539. dev_warn(&h->pdev->dev,
  1540. "maximum logical LUNs (%d) exceeded. "
  1541. "%d LUNs ignored.\n", HPSA_MAX_LUN,
  1542. *nlogicals - HPSA_MAX_LUN);
  1543. *nlogicals = HPSA_MAX_LUN;
  1544. }
  1545. if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
  1546. dev_warn(&h->pdev->dev,
  1547. "maximum logical + physical LUNs (%d) exceeded. "
  1548. "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
  1549. *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
  1550. *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
  1551. }
  1552. return 0;
  1553. }
  1554. u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
  1555. int nphysicals, int nlogicals, struct ReportLUNdata *physdev_list,
  1556. struct ReportLUNdata *logdev_list)
  1557. {
  1558. /* Helper function, figure out where the LUN ID info is coming from
  1559. * given index i, lists of physical and logical devices, where in
  1560. * the list the raid controller is supposed to appear (first or last)
  1561. */
  1562. int logicals_start = nphysicals + (raid_ctlr_position == 0);
  1563. int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
  1564. if (i == raid_ctlr_position)
  1565. return RAID_CTLR_LUNID;
  1566. if (i < logicals_start)
  1567. return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
  1568. if (i < last_device)
  1569. return &logdev_list->LUN[i - nphysicals -
  1570. (raid_ctlr_position == 0)][0];
  1571. BUG();
  1572. return NULL;
  1573. }
  1574. static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
  1575. {
  1576. /* the idea here is we could get notified
  1577. * that some devices have changed, so we do a report
  1578. * physical luns and report logical luns cmd, and adjust
  1579. * our list of devices accordingly.
  1580. *
  1581. * The scsi3addr's of devices won't change so long as the
  1582. * adapter is not reset. That means we can rescan and
  1583. * tell which devices we already know about, vs. new
  1584. * devices, vs. disappearing devices.
  1585. */
  1586. struct ReportLUNdata *physdev_list = NULL;
  1587. struct ReportLUNdata *logdev_list = NULL;
  1588. unsigned char *inq_buff = NULL;
  1589. u32 nphysicals = 0;
  1590. u32 nlogicals = 0;
  1591. u32 ndev_allocated = 0;
  1592. struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
  1593. int ncurrent = 0;
  1594. int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 8;
  1595. int i, nmsa2xxx_enclosures, ndevs_to_allocate;
  1596. int bus, target, lun;
  1597. int raid_ctlr_position;
  1598. DECLARE_BITMAP(lunzerobits, HPSA_MAX_TARGETS_PER_CTLR);
  1599. currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_SCSI_DEVS_PER_HBA,
  1600. GFP_KERNEL);
  1601. physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
  1602. logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
  1603. inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
  1604. tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
  1605. if (!currentsd || !physdev_list || !logdev_list ||
  1606. !inq_buff || !tmpdevice) {
  1607. dev_err(&h->pdev->dev, "out of memory\n");
  1608. goto out;
  1609. }
  1610. memset(lunzerobits, 0, sizeof(lunzerobits));
  1611. if (hpsa_gather_lun_info(h, reportlunsize, physdev_list, &nphysicals,
  1612. logdev_list, &nlogicals))
  1613. goto out;
  1614. /* We might see up to 32 MSA2xxx enclosures, actually 8 of them
  1615. * but each of them 4 times through different paths. The plus 1
  1616. * is for the RAID controller.
  1617. */
  1618. ndevs_to_allocate = nphysicals + nlogicals + MAX_MSA2XXX_ENCLOSURES + 1;
  1619. /* Allocate the per device structures */
  1620. for (i = 0; i < ndevs_to_allocate; i++) {
  1621. currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
  1622. if (!currentsd[i]) {
  1623. dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
  1624. __FILE__, __LINE__);
  1625. goto out;
  1626. }
  1627. ndev_allocated++;
  1628. }
  1629. if (unlikely(is_scsi_rev_5(h)))
  1630. raid_ctlr_position = 0;
  1631. else
  1632. raid_ctlr_position = nphysicals + nlogicals;
  1633. /* adjust our table of devices */
  1634. nmsa2xxx_enclosures = 0;
  1635. for (i = 0; i < nphysicals + nlogicals + 1; i++) {
  1636. u8 *lunaddrbytes;
  1637. /* Figure out where the LUN ID info is coming from */
  1638. lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
  1639. i, nphysicals, nlogicals, physdev_list, logdev_list);
  1640. /* skip masked physical devices. */
  1641. if (lunaddrbytes[3] & 0xC0 &&
  1642. i < nphysicals + (raid_ctlr_position == 0))
  1643. continue;
  1644. /* Get device type, vendor, model, device id */
  1645. if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice))
  1646. continue; /* skip it if we can't talk to it. */
  1647. figure_bus_target_lun(h, lunaddrbytes, &bus, &target, &lun,
  1648. tmpdevice);
  1649. this_device = currentsd[ncurrent];
  1650. /*
  1651. * For the msa2xxx boxes, we have to insert a LUN 0 which
  1652. * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
  1653. * is nonetheless an enclosure device there. We have to
  1654. * present that otherwise linux won't find anything if
  1655. * there is no lun 0.
  1656. */
  1657. if (add_msa2xxx_enclosure_device(h, tmpdevice, this_device,
  1658. lunaddrbytes, bus, target, lun, lunzerobits,
  1659. &nmsa2xxx_enclosures)) {
  1660. ncurrent++;
  1661. this_device = currentsd[ncurrent];
  1662. }
  1663. *this_device = *tmpdevice;
  1664. hpsa_set_bus_target_lun(this_device, bus, target, lun);
  1665. switch (this_device->devtype) {
  1666. case TYPE_ROM: {
  1667. /* We don't *really* support actual CD-ROM devices,
  1668. * just "One Button Disaster Recovery" tape drive
  1669. * which temporarily pretends to be a CD-ROM drive.
  1670. * So we check that the device is really an OBDR tape
  1671. * device by checking for "$DR-10" in bytes 43-48 of
  1672. * the inquiry data.
  1673. */
  1674. char obdr_sig[7];
  1675. #define OBDR_TAPE_SIG "$DR-10"
  1676. strncpy(obdr_sig, &inq_buff[43], 6);
  1677. obdr_sig[6] = '\0';
  1678. if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0)
  1679. /* Not OBDR device, ignore it. */
  1680. break;
  1681. }
  1682. ncurrent++;
  1683. break;
  1684. case TYPE_DISK:
  1685. if (i < nphysicals)
  1686. break;
  1687. ncurrent++;
  1688. break;
  1689. case TYPE_TAPE:
  1690. case TYPE_MEDIUM_CHANGER:
  1691. ncurrent++;
  1692. break;
  1693. case TYPE_RAID:
  1694. /* Only present the Smartarray HBA as a RAID controller.
  1695. * If it's a RAID controller other than the HBA itself
  1696. * (an external RAID controller, MSA500 or similar)
  1697. * don't present it.
  1698. */
  1699. if (!is_hba_lunid(lunaddrbytes))
  1700. break;
  1701. ncurrent++;
  1702. break;
  1703. default:
  1704. break;
  1705. }
  1706. if (ncurrent >= HPSA_MAX_SCSI_DEVS_PER_HBA)
  1707. break;
  1708. }
  1709. adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
  1710. out:
  1711. kfree(tmpdevice);
  1712. for (i = 0; i < ndev_allocated; i++)
  1713. kfree(currentsd[i]);
  1714. kfree(currentsd);
  1715. kfree(inq_buff);
  1716. kfree(physdev_list);
  1717. kfree(logdev_list);
  1718. }
  1719. /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
  1720. * dma mapping and fills in the scatter gather entries of the
  1721. * hpsa command, cp.
  1722. */
  1723. static int hpsa_scatter_gather(struct pci_dev *pdev,
  1724. struct CommandList *cp,
  1725. struct scsi_cmnd *cmd)
  1726. {
  1727. unsigned int len;
  1728. struct scatterlist *sg;
  1729. u64 addr64;
  1730. int use_sg, i;
  1731. BUG_ON(scsi_sg_count(cmd) > MAXSGENTRIES);
  1732. use_sg = scsi_dma_map(cmd);
  1733. if (use_sg < 0)
  1734. return use_sg;
  1735. if (!use_sg)
  1736. goto sglist_finished;
  1737. scsi_for_each_sg(cmd, sg, use_sg, i) {
  1738. addr64 = (u64) sg_dma_address(sg);
  1739. len = sg_dma_len(sg);
  1740. cp->SG[i].Addr.lower =
  1741. (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
  1742. cp->SG[i].Addr.upper =
  1743. (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
  1744. cp->SG[i].Len = len;
  1745. cp->SG[i].Ext = 0; /* we are not chaining */
  1746. }
  1747. sglist_finished:
  1748. cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
  1749. cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
  1750. return 0;
  1751. }
  1752. static int hpsa_scsi_queue_command(struct scsi_cmnd *cmd,
  1753. void (*done)(struct scsi_cmnd *))
  1754. {
  1755. struct ctlr_info *h;
  1756. struct hpsa_scsi_dev_t *dev;
  1757. unsigned char scsi3addr[8];
  1758. struct CommandList *c;
  1759. unsigned long flags;
  1760. /* Get the ptr to our adapter structure out of cmd->host. */
  1761. h = sdev_to_hba(cmd->device);
  1762. dev = cmd->device->hostdata;
  1763. if (!dev) {
  1764. cmd->result = DID_NO_CONNECT << 16;
  1765. done(cmd);
  1766. return 0;
  1767. }
  1768. memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
  1769. /* Need a lock as this is being allocated from the pool */
  1770. spin_lock_irqsave(&h->lock, flags);
  1771. c = cmd_alloc(h);
  1772. spin_unlock_irqrestore(&h->lock, flags);
  1773. if (c == NULL) { /* trouble... */
  1774. dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
  1775. return SCSI_MLQUEUE_HOST_BUSY;
  1776. }
  1777. /* Fill in the command list header */
  1778. cmd->scsi_done = done; /* save this for use by completion code */
  1779. /* save c in case we have to abort it */
  1780. cmd->host_scribble = (unsigned char *) c;
  1781. c->cmd_type = CMD_SCSI;
  1782. c->scsi_cmd = cmd;
  1783. c->Header.ReplyQueue = 0; /* unused in simple mode */
  1784. memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
  1785. c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
  1786. c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
  1787. /* Fill in the request block... */
  1788. c->Request.Timeout = 0;
  1789. memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
  1790. BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
  1791. c->Request.CDBLen = cmd->cmd_len;
  1792. memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
  1793. c->Request.Type.Type = TYPE_CMD;
  1794. c->Request.Type.Attribute = ATTR_SIMPLE;
  1795. switch (cmd->sc_data_direction) {
  1796. case DMA_TO_DEVICE:
  1797. c->Request.Type.Direction = XFER_WRITE;
  1798. break;
  1799. case DMA_FROM_DEVICE:
  1800. c->Request.Type.Direction = XFER_READ;
  1801. break;
  1802. case DMA_NONE:
  1803. c->Request.Type.Direction = XFER_NONE;
  1804. break;
  1805. case DMA_BIDIRECTIONAL:
  1806. /* This can happen if a buggy application does a scsi passthru
  1807. * and sets both inlen and outlen to non-zero. ( see
  1808. * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
  1809. */
  1810. c->Request.Type.Direction = XFER_RSVD;
  1811. /* This is technically wrong, and hpsa controllers should
  1812. * reject it with CMD_INVALID, which is the most correct
  1813. * response, but non-fibre backends appear to let it
  1814. * slide by, and give the same results as if this field
  1815. * were set correctly. Either way is acceptable for
  1816. * our purposes here.
  1817. */
  1818. break;
  1819. default:
  1820. dev_err(&h->pdev->dev, "unknown data direction: %d\n",
  1821. cmd->sc_data_direction);
  1822. BUG();
  1823. break;
  1824. }
  1825. if (hpsa_scatter_gather(h->pdev, c, cmd) < 0) { /* Fill SG list */
  1826. cmd_free(h, c);
  1827. return SCSI_MLQUEUE_HOST_BUSY;
  1828. }
  1829. enqueue_cmd_and_start_io(h, c);
  1830. /* the cmd'll come back via intr handler in complete_scsi_command() */
  1831. return 0;
  1832. }
  1833. static void hpsa_scan_start(struct Scsi_Host *sh)
  1834. {
  1835. struct ctlr_info *h = shost_to_hba(sh);
  1836. unsigned long flags;
  1837. /* wait until any scan already in progress is finished. */
  1838. while (1) {
  1839. spin_lock_irqsave(&h->scan_lock, flags);
  1840. if (h->scan_finished)
  1841. break;
  1842. spin_unlock_irqrestore(&h->scan_lock, flags);
  1843. wait_event(h->scan_wait_queue, h->scan_finished);
  1844. /* Note: We don't need to worry about a race between this
  1845. * thread and driver unload because the midlayer will
  1846. * have incremented the reference count, so unload won't
  1847. * happen if we're in here.
  1848. */
  1849. }
  1850. h->scan_finished = 0; /* mark scan as in progress */
  1851. spin_unlock_irqrestore(&h->scan_lock, flags);
  1852. hpsa_update_scsi_devices(h, h->scsi_host->host_no);
  1853. spin_lock_irqsave(&h->scan_lock, flags);
  1854. h->scan_finished = 1; /* mark scan as finished. */
  1855. wake_up_all(&h->scan_wait_queue);
  1856. spin_unlock_irqrestore(&h->scan_lock, flags);
  1857. }
  1858. static int hpsa_scan_finished(struct Scsi_Host *sh,
  1859. unsigned long elapsed_time)
  1860. {
  1861. struct ctlr_info *h = shost_to_hba(sh);
  1862. unsigned long flags;
  1863. int finished;
  1864. spin_lock_irqsave(&h->scan_lock, flags);
  1865. finished = h->scan_finished;
  1866. spin_unlock_irqrestore(&h->scan_lock, flags);
  1867. return finished;
  1868. }
  1869. static int hpsa_change_queue_depth(struct scsi_device *sdev,
  1870. int qdepth, int reason)
  1871. {
  1872. struct ctlr_info *h = sdev_to_hba(sdev);
  1873. if (reason != SCSI_QDEPTH_DEFAULT)
  1874. return -ENOTSUPP;
  1875. if (qdepth < 1)
  1876. qdepth = 1;
  1877. else
  1878. if (qdepth > h->nr_cmds)
  1879. qdepth = h->nr_cmds;
  1880. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  1881. return sdev->queue_depth;
  1882. }
  1883. static void hpsa_unregister_scsi(struct ctlr_info *h)
  1884. {
  1885. /* we are being forcibly unloaded, and may not refuse. */
  1886. scsi_remove_host(h->scsi_host);
  1887. scsi_host_put(h->scsi_host);
  1888. h->scsi_host = NULL;
  1889. }
  1890. static int hpsa_register_scsi(struct ctlr_info *h)
  1891. {
  1892. int rc;
  1893. rc = hpsa_scsi_detect(h);
  1894. if (rc != 0)
  1895. dev_err(&h->pdev->dev, "hpsa_register_scsi: failed"
  1896. " hpsa_scsi_detect(), rc is %d\n", rc);
  1897. return rc;
  1898. }
  1899. static int wait_for_device_to_become_ready(struct ctlr_info *h,
  1900. unsigned char lunaddr[])
  1901. {
  1902. int rc = 0;
  1903. int count = 0;
  1904. int waittime = 1; /* seconds */
  1905. struct CommandList *c;
  1906. c = cmd_special_alloc(h);
  1907. if (!c) {
  1908. dev_warn(&h->pdev->dev, "out of memory in "
  1909. "wait_for_device_to_become_ready.\n");
  1910. return IO_ERROR;
  1911. }
  1912. /* Send test unit ready until device ready, or give up. */
  1913. while (count < HPSA_TUR_RETRY_LIMIT) {
  1914. /* Wait for a bit. do this first, because if we send
  1915. * the TUR right away, the reset will just abort it.
  1916. */
  1917. msleep(1000 * waittime);
  1918. count++;
  1919. /* Increase wait time with each try, up to a point. */
  1920. if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
  1921. waittime = waittime * 2;
  1922. /* Send the Test Unit Ready */
  1923. fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, lunaddr, TYPE_CMD);
  1924. hpsa_scsi_do_simple_cmd_core(h, c);
  1925. /* no unmap needed here because no data xfer. */
  1926. if (c->err_info->CommandStatus == CMD_SUCCESS)
  1927. break;
  1928. if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
  1929. c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
  1930. (c->err_info->SenseInfo[2] == NO_SENSE ||
  1931. c->err_info->SenseInfo[2] == UNIT_ATTENTION))
  1932. break;
  1933. dev_warn(&h->pdev->dev, "waiting %d secs "
  1934. "for device to become ready.\n", waittime);
  1935. rc = 1; /* device not ready. */
  1936. }
  1937. if (rc)
  1938. dev_warn(&h->pdev->dev, "giving up on device.\n");
  1939. else
  1940. dev_warn(&h->pdev->dev, "device is ready.\n");
  1941. cmd_special_free(h, c);
  1942. return rc;
  1943. }
  1944. /* Need at least one of these error handlers to keep ../scsi/hosts.c from
  1945. * complaining. Doing a host- or bus-reset can't do anything good here.
  1946. */
  1947. static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
  1948. {
  1949. int rc;
  1950. struct ctlr_info *h;
  1951. struct hpsa_scsi_dev_t *dev;
  1952. /* find the controller to which the command to be aborted was sent */
  1953. h = sdev_to_hba(scsicmd->device);
  1954. if (h == NULL) /* paranoia */
  1955. return FAILED;
  1956. dev = scsicmd->device->hostdata;
  1957. if (!dev) {
  1958. dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
  1959. "device lookup failed.\n");
  1960. return FAILED;
  1961. }
  1962. dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
  1963. h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
  1964. /* send a reset to the SCSI LUN which the command was sent to */
  1965. rc = hpsa_send_reset(h, dev->scsi3addr);
  1966. if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
  1967. return SUCCESS;
  1968. dev_warn(&h->pdev->dev, "resetting device failed.\n");
  1969. return FAILED;
  1970. }
  1971. /*
  1972. * For operations that cannot sleep, a command block is allocated at init,
  1973. * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
  1974. * which ones are free or in use. Lock must be held when calling this.
  1975. * cmd_free() is the complement.
  1976. */
  1977. static struct CommandList *cmd_alloc(struct ctlr_info *h)
  1978. {
  1979. struct CommandList *c;
  1980. int i;
  1981. union u64bit temp64;
  1982. dma_addr_t cmd_dma_handle, err_dma_handle;
  1983. do {
  1984. i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
  1985. if (i == h->nr_cmds)
  1986. return NULL;
  1987. } while (test_and_set_bit
  1988. (i & (BITS_PER_LONG - 1),
  1989. h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
  1990. c = h->cmd_pool + i;
  1991. memset(c, 0, sizeof(*c));
  1992. cmd_dma_handle = h->cmd_pool_dhandle
  1993. + i * sizeof(*c);
  1994. c->err_info = h->errinfo_pool + i;
  1995. memset(c->err_info, 0, sizeof(*c->err_info));
  1996. err_dma_handle = h->errinfo_pool_dhandle
  1997. + i * sizeof(*c->err_info);
  1998. h->nr_allocs++;
  1999. c->cmdindex = i;
  2000. INIT_HLIST_NODE(&c->list);
  2001. c->busaddr = (u32) cmd_dma_handle;
  2002. temp64.val = (u64) err_dma_handle;
  2003. c->ErrDesc.Addr.lower = temp64.val32.lower;
  2004. c->ErrDesc.Addr.upper = temp64.val32.upper;
  2005. c->ErrDesc.Len = sizeof(*c->err_info);
  2006. c->h = h;
  2007. return c;
  2008. }
  2009. /* For operations that can wait for kmalloc to possibly sleep,
  2010. * this routine can be called. Lock need not be held to call
  2011. * cmd_special_alloc. cmd_special_free() is the complement.
  2012. */
  2013. static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
  2014. {
  2015. struct CommandList *c;
  2016. union u64bit temp64;
  2017. dma_addr_t cmd_dma_handle, err_dma_handle;
  2018. c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
  2019. if (c == NULL)
  2020. return NULL;
  2021. memset(c, 0, sizeof(*c));
  2022. c->cmdindex = -1;
  2023. c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
  2024. &err_dma_handle);
  2025. if (c->err_info == NULL) {
  2026. pci_free_consistent(h->pdev,
  2027. sizeof(*c), c, cmd_dma_handle);
  2028. return NULL;
  2029. }
  2030. memset(c->err_info, 0, sizeof(*c->err_info));
  2031. INIT_HLIST_NODE(&c->list);
  2032. c->busaddr = (u32) cmd_dma_handle;
  2033. temp64.val = (u64) err_dma_handle;
  2034. c->ErrDesc.Addr.lower = temp64.val32.lower;
  2035. c->ErrDesc.Addr.upper = temp64.val32.upper;
  2036. c->ErrDesc.Len = sizeof(*c->err_info);
  2037. c->h = h;
  2038. return c;
  2039. }
  2040. static void cmd_free(struct ctlr_info *h, struct CommandList *c)
  2041. {
  2042. int i;
  2043. i = c - h->cmd_pool;
  2044. clear_bit(i & (BITS_PER_LONG - 1),
  2045. h->cmd_pool_bits + (i / BITS_PER_LONG));
  2046. h->nr_frees++;
  2047. }
  2048. static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
  2049. {
  2050. union u64bit temp64;
  2051. temp64.val32.lower = c->ErrDesc.Addr.lower;
  2052. temp64.val32.upper = c->ErrDesc.Addr.upper;
  2053. pci_free_consistent(h->pdev, sizeof(*c->err_info),
  2054. c->err_info, (dma_addr_t) temp64.val);
  2055. pci_free_consistent(h->pdev, sizeof(*c),
  2056. c, (dma_addr_t) c->busaddr);
  2057. }
  2058. #ifdef CONFIG_COMPAT
  2059. static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
  2060. {
  2061. IOCTL32_Command_struct __user *arg32 =
  2062. (IOCTL32_Command_struct __user *) arg;
  2063. IOCTL_Command_struct arg64;
  2064. IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
  2065. int err;
  2066. u32 cp;
  2067. err = 0;
  2068. err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  2069. sizeof(arg64.LUN_info));
  2070. err |= copy_from_user(&arg64.Request, &arg32->Request,
  2071. sizeof(arg64.Request));
  2072. err |= copy_from_user(&arg64.error_info, &arg32->error_info,
  2073. sizeof(arg64.error_info));
  2074. err |= get_user(arg64.buf_size, &arg32->buf_size);
  2075. err |= get_user(cp, &arg32->buf);
  2076. arg64.buf = compat_ptr(cp);
  2077. err |= copy_to_user(p, &arg64, sizeof(arg64));
  2078. if (err)
  2079. return -EFAULT;
  2080. err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
  2081. if (err)
  2082. return err;
  2083. err |= copy_in_user(&arg32->error_info, &p->error_info,
  2084. sizeof(arg32->error_info));
  2085. if (err)
  2086. return -EFAULT;
  2087. return err;
  2088. }
  2089. static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
  2090. int cmd, void *arg)
  2091. {
  2092. BIG_IOCTL32_Command_struct __user *arg32 =
  2093. (BIG_IOCTL32_Command_struct __user *) arg;
  2094. BIG_IOCTL_Command_struct arg64;
  2095. BIG_IOCTL_Command_struct __user *p =
  2096. compat_alloc_user_space(sizeof(arg64));
  2097. int err;
  2098. u32 cp;
  2099. err = 0;
  2100. err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  2101. sizeof(arg64.LUN_info));
  2102. err |= copy_from_user(&arg64.Request, &arg32->Request,
  2103. sizeof(arg64.Request));
  2104. err |= copy_from_user(&arg64.error_info, &arg32->error_info,
  2105. sizeof(arg64.error_info));
  2106. err |= get_user(arg64.buf_size, &arg32->buf_size);
  2107. err |= get_user(arg64.malloc_size, &arg32->malloc_size);
  2108. err |= get_user(cp, &arg32->buf);
  2109. arg64.buf = compat_ptr(cp);
  2110. err |= copy_to_user(p, &arg64, sizeof(arg64));
  2111. if (err)
  2112. return -EFAULT;
  2113. err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
  2114. if (err)
  2115. return err;
  2116. err |= copy_in_user(&arg32->error_info, &p->error_info,
  2117. sizeof(arg32->error_info));
  2118. if (err)
  2119. return -EFAULT;
  2120. return err;
  2121. }
  2122. static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
  2123. {
  2124. switch (cmd) {
  2125. case CCISS_GETPCIINFO:
  2126. case CCISS_GETINTINFO:
  2127. case CCISS_SETINTINFO:
  2128. case CCISS_GETNODENAME:
  2129. case CCISS_SETNODENAME:
  2130. case CCISS_GETHEARTBEAT:
  2131. case CCISS_GETBUSTYPES:
  2132. case CCISS_GETFIRMVER:
  2133. case CCISS_GETDRIVVER:
  2134. case CCISS_REVALIDVOLS:
  2135. case CCISS_DEREGDISK:
  2136. case CCISS_REGNEWDISK:
  2137. case CCISS_REGNEWD:
  2138. case CCISS_RESCANDISK:
  2139. case CCISS_GETLUNINFO:
  2140. return hpsa_ioctl(dev, cmd, arg);
  2141. case CCISS_PASSTHRU32:
  2142. return hpsa_ioctl32_passthru(dev, cmd, arg);
  2143. case CCISS_BIG_PASSTHRU32:
  2144. return hpsa_ioctl32_big_passthru(dev, cmd, arg);
  2145. default:
  2146. return -ENOIOCTLCMD;
  2147. }
  2148. }
  2149. #endif
  2150. static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
  2151. {
  2152. struct hpsa_pci_info pciinfo;
  2153. if (!argp)
  2154. return -EINVAL;
  2155. pciinfo.domain = pci_domain_nr(h->pdev->bus);
  2156. pciinfo.bus = h->pdev->bus->number;
  2157. pciinfo.dev_fn = h->pdev->devfn;
  2158. pciinfo.board_id = h->board_id;
  2159. if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
  2160. return -EFAULT;
  2161. return 0;
  2162. }
  2163. static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
  2164. {
  2165. DriverVer_type DriverVer;
  2166. unsigned char vmaj, vmin, vsubmin;
  2167. int rc;
  2168. rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
  2169. &vmaj, &vmin, &vsubmin);
  2170. if (rc != 3) {
  2171. dev_info(&h->pdev->dev, "driver version string '%s' "
  2172. "unrecognized.", HPSA_DRIVER_VERSION);
  2173. vmaj = 0;
  2174. vmin = 0;
  2175. vsubmin = 0;
  2176. }
  2177. DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
  2178. if (!argp)
  2179. return -EINVAL;
  2180. if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
  2181. return -EFAULT;
  2182. return 0;
  2183. }
  2184. static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
  2185. {
  2186. IOCTL_Command_struct iocommand;
  2187. struct CommandList *c;
  2188. char *buff = NULL;
  2189. union u64bit temp64;
  2190. if (!argp)
  2191. return -EINVAL;
  2192. if (!capable(CAP_SYS_RAWIO))
  2193. return -EPERM;
  2194. if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
  2195. return -EFAULT;
  2196. if ((iocommand.buf_size < 1) &&
  2197. (iocommand.Request.Type.Direction != XFER_NONE)) {
  2198. return -EINVAL;
  2199. }
  2200. if (iocommand.buf_size > 0) {
  2201. buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
  2202. if (buff == NULL)
  2203. return -EFAULT;
  2204. }
  2205. if (iocommand.Request.Type.Direction == XFER_WRITE) {
  2206. /* Copy the data into the buffer we created */
  2207. if (copy_from_user(buff, iocommand.buf, iocommand.buf_size)) {
  2208. kfree(buff);
  2209. return -EFAULT;
  2210. }
  2211. } else
  2212. memset(buff, 0, iocommand.buf_size);
  2213. c = cmd_special_alloc(h);
  2214. if (c == NULL) {
  2215. kfree(buff);
  2216. return -ENOMEM;
  2217. }
  2218. /* Fill in the command type */
  2219. c->cmd_type = CMD_IOCTL_PEND;
  2220. /* Fill in Command Header */
  2221. c->Header.ReplyQueue = 0; /* unused in simple mode */
  2222. if (iocommand.buf_size > 0) { /* buffer to fill */
  2223. c->Header.SGList = 1;
  2224. c->Header.SGTotal = 1;
  2225. } else { /* no buffers to fill */
  2226. c->Header.SGList = 0;
  2227. c->Header.SGTotal = 0;
  2228. }
  2229. memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
  2230. /* use the kernel address the cmd block for tag */
  2231. c->Header.Tag.lower = c->busaddr;
  2232. /* Fill in Request block */
  2233. memcpy(&c->Request, &iocommand.Request,
  2234. sizeof(c->Request));
  2235. /* Fill in the scatter gather information */
  2236. if (iocommand.buf_size > 0) {
  2237. temp64.val = pci_map_single(h->pdev, buff,
  2238. iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
  2239. c->SG[0].Addr.lower = temp64.val32.lower;
  2240. c->SG[0].Addr.upper = temp64.val32.upper;
  2241. c->SG[0].Len = iocommand.buf_size;
  2242. c->SG[0].Ext = 0; /* we are not chaining*/
  2243. }
  2244. hpsa_scsi_do_simple_cmd_core(h, c);
  2245. hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
  2246. check_ioctl_unit_attention(h, c);
  2247. /* Copy the error information out */
  2248. memcpy(&iocommand.error_info, c->err_info,
  2249. sizeof(iocommand.error_info));
  2250. if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
  2251. kfree(buff);
  2252. cmd_special_free(h, c);
  2253. return -EFAULT;
  2254. }
  2255. if (iocommand.Request.Type.Direction == XFER_READ) {
  2256. /* Copy the data out of the buffer we created */
  2257. if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
  2258. kfree(buff);
  2259. cmd_special_free(h, c);
  2260. return -EFAULT;
  2261. }
  2262. }
  2263. kfree(buff);
  2264. cmd_special_free(h, c);
  2265. return 0;
  2266. }
  2267. static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
  2268. {
  2269. BIG_IOCTL_Command_struct *ioc;
  2270. struct CommandList *c;
  2271. unsigned char **buff = NULL;
  2272. int *buff_size = NULL;
  2273. union u64bit temp64;
  2274. BYTE sg_used = 0;
  2275. int status = 0;
  2276. int i;
  2277. u32 left;
  2278. u32 sz;
  2279. BYTE __user *data_ptr;
  2280. if (!argp)
  2281. return -EINVAL;
  2282. if (!capable(CAP_SYS_RAWIO))
  2283. return -EPERM;
  2284. ioc = (BIG_IOCTL_Command_struct *)
  2285. kmalloc(sizeof(*ioc), GFP_KERNEL);
  2286. if (!ioc) {
  2287. status = -ENOMEM;
  2288. goto cleanup1;
  2289. }
  2290. if (copy_from_user(ioc, argp, sizeof(*ioc))) {
  2291. status = -EFAULT;
  2292. goto cleanup1;
  2293. }
  2294. if ((ioc->buf_size < 1) &&
  2295. (ioc->Request.Type.Direction != XFER_NONE)) {
  2296. status = -EINVAL;
  2297. goto cleanup1;
  2298. }
  2299. /* Check kmalloc limits using all SGs */
  2300. if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
  2301. status = -EINVAL;
  2302. goto cleanup1;
  2303. }
  2304. if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
  2305. status = -EINVAL;
  2306. goto cleanup1;
  2307. }
  2308. buff = kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
  2309. if (!buff) {
  2310. status = -ENOMEM;
  2311. goto cleanup1;
  2312. }
  2313. buff_size = kmalloc(MAXSGENTRIES * sizeof(int), GFP_KERNEL);
  2314. if (!buff_size) {
  2315. status = -ENOMEM;
  2316. goto cleanup1;
  2317. }
  2318. left = ioc->buf_size;
  2319. data_ptr = ioc->buf;
  2320. while (left) {
  2321. sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
  2322. buff_size[sg_used] = sz;
  2323. buff[sg_used] = kmalloc(sz, GFP_KERNEL);
  2324. if (buff[sg_used] == NULL) {
  2325. status = -ENOMEM;
  2326. goto cleanup1;
  2327. }
  2328. if (ioc->Request.Type.Direction == XFER_WRITE) {
  2329. if (copy_from_user(buff[sg_used], data_ptr, sz)) {
  2330. status = -ENOMEM;
  2331. goto cleanup1;
  2332. }
  2333. } else
  2334. memset(buff[sg_used], 0, sz);
  2335. left -= sz;
  2336. data_ptr += sz;
  2337. sg_used++;
  2338. }
  2339. c = cmd_special_alloc(h);
  2340. if (c == NULL) {
  2341. status = -ENOMEM;
  2342. goto cleanup1;
  2343. }
  2344. c->cmd_type = CMD_IOCTL_PEND;
  2345. c->Header.ReplyQueue = 0;
  2346. if (ioc->buf_size > 0) {
  2347. c->Header.SGList = sg_used;
  2348. c->Header.SGTotal = sg_used;
  2349. } else {
  2350. c->Header.SGList = 0;
  2351. c->Header.SGTotal = 0;
  2352. }
  2353. memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
  2354. c->Header.Tag.lower = c->busaddr;
  2355. memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
  2356. if (ioc->buf_size > 0) {
  2357. int i;
  2358. for (i = 0; i < sg_used; i++) {
  2359. temp64.val = pci_map_single(h->pdev, buff[i],
  2360. buff_size[i], PCI_DMA_BIDIRECTIONAL);
  2361. c->SG[i].Addr.lower = temp64.val32.lower;
  2362. c->SG[i].Addr.upper = temp64.val32.upper;
  2363. c->SG[i].Len = buff_size[i];
  2364. /* we are not chaining */
  2365. c->SG[i].Ext = 0;
  2366. }
  2367. }
  2368. hpsa_scsi_do_simple_cmd_core(h, c);
  2369. hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
  2370. check_ioctl_unit_attention(h, c);
  2371. /* Copy the error information out */
  2372. memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
  2373. if (copy_to_user(argp, ioc, sizeof(*ioc))) {
  2374. cmd_special_free(h, c);
  2375. status = -EFAULT;
  2376. goto cleanup1;
  2377. }
  2378. if (ioc->Request.Type.Direction == XFER_READ) {
  2379. /* Copy the data out of the buffer we created */
  2380. BYTE __user *ptr = ioc->buf;
  2381. for (i = 0; i < sg_used; i++) {
  2382. if (copy_to_user(ptr, buff[i], buff_size[i])) {
  2383. cmd_special_free(h, c);
  2384. status = -EFAULT;
  2385. goto cleanup1;
  2386. }
  2387. ptr += buff_size[i];
  2388. }
  2389. }
  2390. cmd_special_free(h, c);
  2391. status = 0;
  2392. cleanup1:
  2393. if (buff) {
  2394. for (i = 0; i < sg_used; i++)
  2395. kfree(buff[i]);
  2396. kfree(buff);
  2397. }
  2398. kfree(buff_size);
  2399. kfree(ioc);
  2400. return status;
  2401. }
  2402. static void check_ioctl_unit_attention(struct ctlr_info *h,
  2403. struct CommandList *c)
  2404. {
  2405. if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
  2406. c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
  2407. (void) check_for_unit_attention(h, c);
  2408. }
  2409. /*
  2410. * ioctl
  2411. */
  2412. static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
  2413. {
  2414. struct ctlr_info *h;
  2415. void __user *argp = (void __user *)arg;
  2416. h = sdev_to_hba(dev);
  2417. switch (cmd) {
  2418. case CCISS_DEREGDISK:
  2419. case CCISS_REGNEWDISK:
  2420. case CCISS_REGNEWD:
  2421. hpsa_scan_start(h->scsi_host);
  2422. return 0;
  2423. case CCISS_GETPCIINFO:
  2424. return hpsa_getpciinfo_ioctl(h, argp);
  2425. case CCISS_GETDRIVVER:
  2426. return hpsa_getdrivver_ioctl(h, argp);
  2427. case CCISS_PASSTHRU:
  2428. return hpsa_passthru_ioctl(h, argp);
  2429. case CCISS_BIG_PASSTHRU:
  2430. return hpsa_big_passthru_ioctl(h, argp);
  2431. default:
  2432. return -ENOTTY;
  2433. }
  2434. }
  2435. static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
  2436. void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
  2437. int cmd_type)
  2438. {
  2439. int pci_dir = XFER_NONE;
  2440. c->cmd_type = CMD_IOCTL_PEND;
  2441. c->Header.ReplyQueue = 0;
  2442. if (buff != NULL && size > 0) {
  2443. c->Header.SGList = 1;
  2444. c->Header.SGTotal = 1;
  2445. } else {
  2446. c->Header.SGList = 0;
  2447. c->Header.SGTotal = 0;
  2448. }
  2449. c->Header.Tag.lower = c->busaddr;
  2450. memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
  2451. c->Request.Type.Type = cmd_type;
  2452. if (cmd_type == TYPE_CMD) {
  2453. switch (cmd) {
  2454. case HPSA_INQUIRY:
  2455. /* are we trying to read a vital product page */
  2456. if (page_code != 0) {
  2457. c->Request.CDB[1] = 0x01;
  2458. c->Request.CDB[2] = page_code;
  2459. }
  2460. c->Request.CDBLen = 6;
  2461. c->Request.Type.Attribute = ATTR_SIMPLE;
  2462. c->Request.Type.Direction = XFER_READ;
  2463. c->Request.Timeout = 0;
  2464. c->Request.CDB[0] = HPSA_INQUIRY;
  2465. c->Request.CDB[4] = size & 0xFF;
  2466. break;
  2467. case HPSA_REPORT_LOG:
  2468. case HPSA_REPORT_PHYS:
  2469. /* Talking to controller so It's a physical command
  2470. mode = 00 target = 0. Nothing to write.
  2471. */
  2472. c->Request.CDBLen = 12;
  2473. c->Request.Type.Attribute = ATTR_SIMPLE;
  2474. c->Request.Type.Direction = XFER_READ;
  2475. c->Request.Timeout = 0;
  2476. c->Request.CDB[0] = cmd;
  2477. c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
  2478. c->Request.CDB[7] = (size >> 16) & 0xFF;
  2479. c->Request.CDB[8] = (size >> 8) & 0xFF;
  2480. c->Request.CDB[9] = size & 0xFF;
  2481. break;
  2482. case HPSA_READ_CAPACITY:
  2483. c->Request.CDBLen = 10;
  2484. c->Request.Type.Attribute = ATTR_SIMPLE;
  2485. c->Request.Type.Direction = XFER_READ;
  2486. c->Request.Timeout = 0;
  2487. c->Request.CDB[0] = cmd;
  2488. break;
  2489. case HPSA_CACHE_FLUSH:
  2490. c->Request.CDBLen = 12;
  2491. c->Request.Type.Attribute = ATTR_SIMPLE;
  2492. c->Request.Type.Direction = XFER_WRITE;
  2493. c->Request.Timeout = 0;
  2494. c->Request.CDB[0] = BMIC_WRITE;
  2495. c->Request.CDB[6] = BMIC_CACHE_FLUSH;
  2496. break;
  2497. case TEST_UNIT_READY:
  2498. c->Request.CDBLen = 6;
  2499. c->Request.Type.Attribute = ATTR_SIMPLE;
  2500. c->Request.Type.Direction = XFER_NONE;
  2501. c->Request.Timeout = 0;
  2502. break;
  2503. default:
  2504. dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
  2505. BUG();
  2506. return;
  2507. }
  2508. } else if (cmd_type == TYPE_MSG) {
  2509. switch (cmd) {
  2510. case HPSA_DEVICE_RESET_MSG:
  2511. c->Request.CDBLen = 16;
  2512. c->Request.Type.Type = 1; /* It is a MSG not a CMD */
  2513. c->Request.Type.Attribute = ATTR_SIMPLE;
  2514. c->Request.Type.Direction = XFER_NONE;
  2515. c->Request.Timeout = 0; /* Don't time out */
  2516. c->Request.CDB[0] = 0x01; /* RESET_MSG is 0x01 */
  2517. c->Request.CDB[1] = 0x03; /* Reset target above */
  2518. /* If bytes 4-7 are zero, it means reset the */
  2519. /* LunID device */
  2520. c->Request.CDB[4] = 0x00;
  2521. c->Request.CDB[5] = 0x00;
  2522. c->Request.CDB[6] = 0x00;
  2523. c->Request.CDB[7] = 0x00;
  2524. break;
  2525. default:
  2526. dev_warn(&h->pdev->dev, "unknown message type %d\n",
  2527. cmd);
  2528. BUG();
  2529. }
  2530. } else {
  2531. dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
  2532. BUG();
  2533. }
  2534. switch (c->Request.Type.Direction) {
  2535. case XFER_READ:
  2536. pci_dir = PCI_DMA_FROMDEVICE;
  2537. break;
  2538. case XFER_WRITE:
  2539. pci_dir = PCI_DMA_TODEVICE;
  2540. break;
  2541. case XFER_NONE:
  2542. pci_dir = PCI_DMA_NONE;
  2543. break;
  2544. default:
  2545. pci_dir = PCI_DMA_BIDIRECTIONAL;
  2546. }
  2547. hpsa_map_one(h->pdev, c, buff, size, pci_dir);
  2548. return;
  2549. }
  2550. /*
  2551. * Map (physical) PCI mem into (virtual) kernel space
  2552. */
  2553. static void __iomem *remap_pci_mem(ulong base, ulong size)
  2554. {
  2555. ulong page_base = ((ulong) base) & PAGE_MASK;
  2556. ulong page_offs = ((ulong) base) - page_base;
  2557. void __iomem *page_remapped = ioremap(page_base, page_offs + size);
  2558. return page_remapped ? (page_remapped + page_offs) : NULL;
  2559. }
  2560. /* Takes cmds off the submission queue and sends them to the hardware,
  2561. * then puts them on the queue of cmds waiting for completion.
  2562. */
  2563. static void start_io(struct ctlr_info *h)
  2564. {
  2565. struct CommandList *c;
  2566. while (!hlist_empty(&h->reqQ)) {
  2567. c = hlist_entry(h->reqQ.first, struct CommandList, list);
  2568. /* can't do anything if fifo is full */
  2569. if ((h->access.fifo_full(h))) {
  2570. dev_warn(&h->pdev->dev, "fifo full\n");
  2571. break;
  2572. }
  2573. /* Get the first entry from the Request Q */
  2574. removeQ(c);
  2575. h->Qdepth--;
  2576. /* Tell the controller execute command */
  2577. h->access.submit_command(h, c);
  2578. /* Put job onto the completed Q */
  2579. addQ(&h->cmpQ, c);
  2580. }
  2581. }
  2582. static inline unsigned long get_next_completion(struct ctlr_info *h)
  2583. {
  2584. return h->access.command_completed(h);
  2585. }
  2586. static inline bool interrupt_pending(struct ctlr_info *h)
  2587. {
  2588. return h->access.intr_pending(h);
  2589. }
  2590. static inline long interrupt_not_for_us(struct ctlr_info *h)
  2591. {
  2592. return !(h->msi_vector || h->msix_vector) &&
  2593. ((h->access.intr_pending(h) == 0) ||
  2594. (h->interrupts_enabled == 0));
  2595. }
  2596. static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
  2597. u32 raw_tag)
  2598. {
  2599. if (unlikely(tag_index >= h->nr_cmds)) {
  2600. dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
  2601. return 1;
  2602. }
  2603. return 0;
  2604. }
  2605. static inline void finish_cmd(struct CommandList *c, u32 raw_tag)
  2606. {
  2607. removeQ(c);
  2608. if (likely(c->cmd_type == CMD_SCSI))
  2609. complete_scsi_command(c, 0, raw_tag);
  2610. else if (c->cmd_type == CMD_IOCTL_PEND)
  2611. complete(c->waiting);
  2612. }
  2613. static inline u32 hpsa_tag_contains_index(u32 tag)
  2614. {
  2615. #define DIRECT_LOOKUP_BIT 0x10
  2616. return tag & DIRECT_LOOKUP_BIT;
  2617. }
  2618. static inline u32 hpsa_tag_to_index(u32 tag)
  2619. {
  2620. #define DIRECT_LOOKUP_SHIFT 5
  2621. return tag >> DIRECT_LOOKUP_SHIFT;
  2622. }
  2623. static inline u32 hpsa_tag_discard_error_bits(u32 tag)
  2624. {
  2625. #define HPSA_ERROR_BITS 0x03
  2626. return tag & ~HPSA_ERROR_BITS;
  2627. }
  2628. /* process completion of an indexed ("direct lookup") command */
  2629. static inline u32 process_indexed_cmd(struct ctlr_info *h,
  2630. u32 raw_tag)
  2631. {
  2632. u32 tag_index;
  2633. struct CommandList *c;
  2634. tag_index = hpsa_tag_to_index(raw_tag);
  2635. if (bad_tag(h, tag_index, raw_tag))
  2636. return next_command(h);
  2637. c = h->cmd_pool + tag_index;
  2638. finish_cmd(c, raw_tag);
  2639. return next_command(h);
  2640. }
  2641. /* process completion of a non-indexed command */
  2642. static inline u32 process_nonindexed_cmd(struct ctlr_info *h,
  2643. u32 raw_tag)
  2644. {
  2645. u32 tag;
  2646. struct CommandList *c = NULL;
  2647. struct hlist_node *tmp;
  2648. tag = hpsa_tag_discard_error_bits(raw_tag);
  2649. hlist_for_each_entry(c, tmp, &h->cmpQ, list) {
  2650. if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
  2651. finish_cmd(c, raw_tag);
  2652. return next_command(h);
  2653. }
  2654. }
  2655. bad_tag(h, h->nr_cmds + 1, raw_tag);
  2656. return next_command(h);
  2657. }
  2658. static irqreturn_t do_hpsa_intr(int irq, void *dev_id)
  2659. {
  2660. struct ctlr_info *h = dev_id;
  2661. unsigned long flags;
  2662. u32 raw_tag;
  2663. if (interrupt_not_for_us(h))
  2664. return IRQ_NONE;
  2665. spin_lock_irqsave(&h->lock, flags);
  2666. raw_tag = get_next_completion(h);
  2667. while (raw_tag != FIFO_EMPTY) {
  2668. if (hpsa_tag_contains_index(raw_tag))
  2669. raw_tag = process_indexed_cmd(h, raw_tag);
  2670. else
  2671. raw_tag = process_nonindexed_cmd(h, raw_tag);
  2672. }
  2673. spin_unlock_irqrestore(&h->lock, flags);
  2674. return IRQ_HANDLED;
  2675. }
  2676. /* Send a message CDB to the firmwart. */
  2677. static __devinit int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
  2678. unsigned char type)
  2679. {
  2680. struct Command {
  2681. struct CommandListHeader CommandHeader;
  2682. struct RequestBlock Request;
  2683. struct ErrDescriptor ErrorDescriptor;
  2684. };
  2685. struct Command *cmd;
  2686. static const size_t cmd_sz = sizeof(*cmd) +
  2687. sizeof(cmd->ErrorDescriptor);
  2688. dma_addr_t paddr64;
  2689. uint32_t paddr32, tag;
  2690. void __iomem *vaddr;
  2691. int i, err;
  2692. vaddr = pci_ioremap_bar(pdev, 0);
  2693. if (vaddr == NULL)
  2694. return -ENOMEM;
  2695. /* The Inbound Post Queue only accepts 32-bit physical addresses for the
  2696. * CCISS commands, so they must be allocated from the lower 4GiB of
  2697. * memory.
  2698. */
  2699. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  2700. if (err) {
  2701. iounmap(vaddr);
  2702. return -ENOMEM;
  2703. }
  2704. cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
  2705. if (cmd == NULL) {
  2706. iounmap(vaddr);
  2707. return -ENOMEM;
  2708. }
  2709. /* This must fit, because of the 32-bit consistent DMA mask. Also,
  2710. * although there's no guarantee, we assume that the address is at
  2711. * least 4-byte aligned (most likely, it's page-aligned).
  2712. */
  2713. paddr32 = paddr64;
  2714. cmd->CommandHeader.ReplyQueue = 0;
  2715. cmd->CommandHeader.SGList = 0;
  2716. cmd->CommandHeader.SGTotal = 0;
  2717. cmd->CommandHeader.Tag.lower = paddr32;
  2718. cmd->CommandHeader.Tag.upper = 0;
  2719. memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
  2720. cmd->Request.CDBLen = 16;
  2721. cmd->Request.Type.Type = TYPE_MSG;
  2722. cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
  2723. cmd->Request.Type.Direction = XFER_NONE;
  2724. cmd->Request.Timeout = 0; /* Don't time out */
  2725. cmd->Request.CDB[0] = opcode;
  2726. cmd->Request.CDB[1] = type;
  2727. memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
  2728. cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
  2729. cmd->ErrorDescriptor.Addr.upper = 0;
  2730. cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
  2731. writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
  2732. for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
  2733. tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
  2734. if (hpsa_tag_discard_error_bits(tag) == paddr32)
  2735. break;
  2736. msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
  2737. }
  2738. iounmap(vaddr);
  2739. /* we leak the DMA buffer here ... no choice since the controller could
  2740. * still complete the command.
  2741. */
  2742. if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
  2743. dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
  2744. opcode, type);
  2745. return -ETIMEDOUT;
  2746. }
  2747. pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
  2748. if (tag & HPSA_ERROR_BIT) {
  2749. dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
  2750. opcode, type);
  2751. return -EIO;
  2752. }
  2753. dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
  2754. opcode, type);
  2755. return 0;
  2756. }
  2757. #define hpsa_soft_reset_controller(p) hpsa_message(p, 1, 0)
  2758. #define hpsa_noop(p) hpsa_message(p, 3, 0)
  2759. static __devinit int hpsa_reset_msi(struct pci_dev *pdev)
  2760. {
  2761. /* the #defines are stolen from drivers/pci/msi.h. */
  2762. #define msi_control_reg(base) (base + PCI_MSI_FLAGS)
  2763. #define PCI_MSIX_FLAGS_ENABLE (1 << 15)
  2764. int pos;
  2765. u16 control = 0;
  2766. pos = pci_find_capability(pdev, PCI_CAP_ID_MSI);
  2767. if (pos) {
  2768. pci_read_config_word(pdev, msi_control_reg(pos), &control);
  2769. if (control & PCI_MSI_FLAGS_ENABLE) {
  2770. dev_info(&pdev->dev, "resetting MSI\n");
  2771. pci_write_config_word(pdev, msi_control_reg(pos),
  2772. control & ~PCI_MSI_FLAGS_ENABLE);
  2773. }
  2774. }
  2775. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  2776. if (pos) {
  2777. pci_read_config_word(pdev, msi_control_reg(pos), &control);
  2778. if (control & PCI_MSIX_FLAGS_ENABLE) {
  2779. dev_info(&pdev->dev, "resetting MSI-X\n");
  2780. pci_write_config_word(pdev, msi_control_reg(pos),
  2781. control & ~PCI_MSIX_FLAGS_ENABLE);
  2782. }
  2783. }
  2784. return 0;
  2785. }
  2786. /* This does a hard reset of the controller using PCI power management
  2787. * states.
  2788. */
  2789. static __devinit int hpsa_hard_reset_controller(struct pci_dev *pdev)
  2790. {
  2791. u16 pmcsr, saved_config_space[32];
  2792. int i, pos;
  2793. dev_info(&pdev->dev, "using PCI PM to reset controller\n");
  2794. /* This is very nearly the same thing as
  2795. *
  2796. * pci_save_state(pci_dev);
  2797. * pci_set_power_state(pci_dev, PCI_D3hot);
  2798. * pci_set_power_state(pci_dev, PCI_D0);
  2799. * pci_restore_state(pci_dev);
  2800. *
  2801. * but we can't use these nice canned kernel routines on
  2802. * kexec, because they also check the MSI/MSI-X state in PCI
  2803. * configuration space and do the wrong thing when it is
  2804. * set/cleared. Also, the pci_save/restore_state functions
  2805. * violate the ordering requirements for restoring the
  2806. * configuration space from the CCISS document (see the
  2807. * comment below). So we roll our own ....
  2808. */
  2809. for (i = 0; i < 32; i++)
  2810. pci_read_config_word(pdev, 2*i, &saved_config_space[i]);
  2811. pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
  2812. if (pos == 0) {
  2813. dev_err(&pdev->dev,
  2814. "hpsa_reset_controller: PCI PM not supported\n");
  2815. return -ENODEV;
  2816. }
  2817. /* Quoting from the Open CISS Specification: "The Power
  2818. * Management Control/Status Register (CSR) controls the power
  2819. * state of the device. The normal operating state is D0,
  2820. * CSR=00h. The software off state is D3, CSR=03h. To reset
  2821. * the controller, place the interface device in D3 then to
  2822. * D0, this causes a secondary PCI reset which will reset the
  2823. * controller."
  2824. */
  2825. /* enter the D3hot power management state */
  2826. pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
  2827. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  2828. pmcsr |= PCI_D3hot;
  2829. pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
  2830. msleep(500);
  2831. /* enter the D0 power management state */
  2832. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  2833. pmcsr |= PCI_D0;
  2834. pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
  2835. msleep(500);
  2836. /* Restore the PCI configuration space. The Open CISS
  2837. * Specification says, "Restore the PCI Configuration
  2838. * Registers, offsets 00h through 60h. It is important to
  2839. * restore the command register, 16-bits at offset 04h,
  2840. * last. Do not restore the configuration status register,
  2841. * 16-bits at offset 06h." Note that the offset is 2*i.
  2842. */
  2843. for (i = 0; i < 32; i++) {
  2844. if (i == 2 || i == 3)
  2845. continue;
  2846. pci_write_config_word(pdev, 2*i, saved_config_space[i]);
  2847. }
  2848. wmb();
  2849. pci_write_config_word(pdev, 4, saved_config_space[2]);
  2850. return 0;
  2851. }
  2852. /*
  2853. * We cannot read the structure directly, for portability we must use
  2854. * the io functions.
  2855. * This is for debug only.
  2856. */
  2857. #ifdef HPSA_DEBUG
  2858. static void print_cfg_table(struct device *dev, struct CfgTable *tb)
  2859. {
  2860. int i;
  2861. char temp_name[17];
  2862. dev_info(dev, "Controller Configuration information\n");
  2863. dev_info(dev, "------------------------------------\n");
  2864. for (i = 0; i < 4; i++)
  2865. temp_name[i] = readb(&(tb->Signature[i]));
  2866. temp_name[4] = '\0';
  2867. dev_info(dev, " Signature = %s\n", temp_name);
  2868. dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
  2869. dev_info(dev, " Transport methods supported = 0x%x\n",
  2870. readl(&(tb->TransportSupport)));
  2871. dev_info(dev, " Transport methods active = 0x%x\n",
  2872. readl(&(tb->TransportActive)));
  2873. dev_info(dev, " Requested transport Method = 0x%x\n",
  2874. readl(&(tb->HostWrite.TransportRequest)));
  2875. dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
  2876. readl(&(tb->HostWrite.CoalIntDelay)));
  2877. dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
  2878. readl(&(tb->HostWrite.CoalIntCount)));
  2879. dev_info(dev, " Max outstanding commands = 0x%d\n",
  2880. readl(&(tb->CmdsOutMax)));
  2881. dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
  2882. for (i = 0; i < 16; i++)
  2883. temp_name[i] = readb(&(tb->ServerName[i]));
  2884. temp_name[16] = '\0';
  2885. dev_info(dev, " Server Name = %s\n", temp_name);
  2886. dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
  2887. readl(&(tb->HeartBeat)));
  2888. }
  2889. #endif /* HPSA_DEBUG */
  2890. static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
  2891. {
  2892. int i, offset, mem_type, bar_type;
  2893. if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
  2894. return 0;
  2895. offset = 0;
  2896. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  2897. bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
  2898. if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
  2899. offset += 4;
  2900. else {
  2901. mem_type = pci_resource_flags(pdev, i) &
  2902. PCI_BASE_ADDRESS_MEM_TYPE_MASK;
  2903. switch (mem_type) {
  2904. case PCI_BASE_ADDRESS_MEM_TYPE_32:
  2905. case PCI_BASE_ADDRESS_MEM_TYPE_1M:
  2906. offset += 4; /* 32 bit */
  2907. break;
  2908. case PCI_BASE_ADDRESS_MEM_TYPE_64:
  2909. offset += 8;
  2910. break;
  2911. default: /* reserved in PCI 2.2 */
  2912. dev_warn(&pdev->dev,
  2913. "base address is invalid\n");
  2914. return -1;
  2915. break;
  2916. }
  2917. }
  2918. if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
  2919. return i + 1;
  2920. }
  2921. return -1;
  2922. }
  2923. /* If MSI/MSI-X is supported by the kernel we will try to enable it on
  2924. * controllers that are capable. If not, we use IO-APIC mode.
  2925. */
  2926. static void __devinit hpsa_interrupt_mode(struct ctlr_info *h,
  2927. struct pci_dev *pdev, u32 board_id)
  2928. {
  2929. #ifdef CONFIG_PCI_MSI
  2930. int err;
  2931. struct msix_entry hpsa_msix_entries[4] = { {0, 0}, {0, 1},
  2932. {0, 2}, {0, 3}
  2933. };
  2934. /* Some boards advertise MSI but don't really support it */
  2935. if ((board_id == 0x40700E11) ||
  2936. (board_id == 0x40800E11) ||
  2937. (board_id == 0x40820E11) || (board_id == 0x40830E11))
  2938. goto default_int_mode;
  2939. if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) {
  2940. dev_info(&pdev->dev, "MSIX\n");
  2941. err = pci_enable_msix(pdev, hpsa_msix_entries, 4);
  2942. if (!err) {
  2943. h->intr[0] = hpsa_msix_entries[0].vector;
  2944. h->intr[1] = hpsa_msix_entries[1].vector;
  2945. h->intr[2] = hpsa_msix_entries[2].vector;
  2946. h->intr[3] = hpsa_msix_entries[3].vector;
  2947. h->msix_vector = 1;
  2948. return;
  2949. }
  2950. if (err > 0) {
  2951. dev_warn(&pdev->dev, "only %d MSI-X vectors "
  2952. "available\n", err);
  2953. goto default_int_mode;
  2954. } else {
  2955. dev_warn(&pdev->dev, "MSI-X init failed %d\n",
  2956. err);
  2957. goto default_int_mode;
  2958. }
  2959. }
  2960. if (pci_find_capability(pdev, PCI_CAP_ID_MSI)) {
  2961. dev_info(&pdev->dev, "MSI\n");
  2962. if (!pci_enable_msi(pdev))
  2963. h->msi_vector = 1;
  2964. else
  2965. dev_warn(&pdev->dev, "MSI init failed\n");
  2966. }
  2967. default_int_mode:
  2968. #endif /* CONFIG_PCI_MSI */
  2969. /* if we get here we're going to use the default interrupt mode */
  2970. h->intr[PERF_MODE_INT] = pdev->irq;
  2971. }
  2972. static int hpsa_pci_init(struct ctlr_info *h, struct pci_dev *pdev)
  2973. {
  2974. ushort subsystem_vendor_id, subsystem_device_id, command;
  2975. u32 board_id, scratchpad = 0;
  2976. u64 cfg_offset;
  2977. u32 cfg_base_addr;
  2978. u64 cfg_base_addr_index;
  2979. u32 trans_offset;
  2980. int i, prod_index, err;
  2981. subsystem_vendor_id = pdev->subsystem_vendor;
  2982. subsystem_device_id = pdev->subsystem_device;
  2983. board_id = (((u32) (subsystem_device_id << 16) & 0xffff0000) |
  2984. subsystem_vendor_id);
  2985. for (i = 0; i < ARRAY_SIZE(products); i++)
  2986. if (board_id == products[i].board_id)
  2987. break;
  2988. prod_index = i;
  2989. if (prod_index == ARRAY_SIZE(products)) {
  2990. prod_index--;
  2991. if (subsystem_vendor_id != PCI_VENDOR_ID_HP ||
  2992. !hpsa_allow_any) {
  2993. dev_warn(&pdev->dev, "unrecognized board ID:"
  2994. " 0x%08lx, ignoring.\n",
  2995. (unsigned long) board_id);
  2996. return -ENODEV;
  2997. }
  2998. }
  2999. /* check to see if controller has been disabled
  3000. * BEFORE trying to enable it
  3001. */
  3002. (void)pci_read_config_word(pdev, PCI_COMMAND, &command);
  3003. if (!(command & 0x02)) {
  3004. dev_warn(&pdev->dev, "controller appears to be disabled\n");
  3005. return -ENODEV;
  3006. }
  3007. err = pci_enable_device(pdev);
  3008. if (err) {
  3009. dev_warn(&pdev->dev, "unable to enable PCI device\n");
  3010. return err;
  3011. }
  3012. err = pci_request_regions(pdev, "hpsa");
  3013. if (err) {
  3014. dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n");
  3015. return err;
  3016. }
  3017. /* If the kernel supports MSI/MSI-X we will try to enable that,
  3018. * else we use the IO-APIC interrupt assigned to us by system ROM.
  3019. */
  3020. hpsa_interrupt_mode(h, pdev, board_id);
  3021. /* find the memory BAR */
  3022. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  3023. if (pci_resource_flags(pdev, i) & IORESOURCE_MEM)
  3024. break;
  3025. }
  3026. if (i == DEVICE_COUNT_RESOURCE) {
  3027. dev_warn(&pdev->dev, "no memory BAR found\n");
  3028. err = -ENODEV;
  3029. goto err_out_free_res;
  3030. }
  3031. h->paddr = pci_resource_start(pdev, i); /* addressing mode bits
  3032. * already removed
  3033. */
  3034. h->vaddr = remap_pci_mem(h->paddr, 0x250);
  3035. /* Wait for the board to become ready. */
  3036. for (i = 0; i < HPSA_BOARD_READY_ITERATIONS; i++) {
  3037. scratchpad = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
  3038. if (scratchpad == HPSA_FIRMWARE_READY)
  3039. break;
  3040. msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
  3041. }
  3042. if (scratchpad != HPSA_FIRMWARE_READY) {
  3043. dev_warn(&pdev->dev, "board not ready, timed out.\n");
  3044. err = -ENODEV;
  3045. goto err_out_free_res;
  3046. }
  3047. /* get the address index number */
  3048. cfg_base_addr = readl(h->vaddr + SA5_CTCFG_OFFSET);
  3049. cfg_base_addr &= (u32) 0x0000ffff;
  3050. cfg_base_addr_index = find_PCI_BAR_index(pdev, cfg_base_addr);
  3051. if (cfg_base_addr_index == -1) {
  3052. dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
  3053. err = -ENODEV;
  3054. goto err_out_free_res;
  3055. }
  3056. cfg_offset = readl(h->vaddr + SA5_CTMEM_OFFSET);
  3057. h->cfgtable = remap_pci_mem(pci_resource_start(pdev,
  3058. cfg_base_addr_index) + cfg_offset,
  3059. sizeof(h->cfgtable));
  3060. /* Find performant mode table. */
  3061. trans_offset = readl(&(h->cfgtable->TransMethodOffset));
  3062. h->transtable = remap_pci_mem(pci_resource_start(pdev,
  3063. cfg_base_addr_index)+cfg_offset+trans_offset,
  3064. sizeof(*h->transtable));
  3065. h->board_id = board_id;
  3066. h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
  3067. h->product_name = products[prod_index].product_name;
  3068. h->access = *(products[prod_index].access);
  3069. /* Allow room for some ioctls */
  3070. h->nr_cmds = h->max_commands - 4;
  3071. if ((readb(&h->cfgtable->Signature[0]) != 'C') ||
  3072. (readb(&h->cfgtable->Signature[1]) != 'I') ||
  3073. (readb(&h->cfgtable->Signature[2]) != 'S') ||
  3074. (readb(&h->cfgtable->Signature[3]) != 'S')) {
  3075. dev_warn(&pdev->dev, "not a valid CISS config table\n");
  3076. err = -ENODEV;
  3077. goto err_out_free_res;
  3078. }
  3079. #ifdef CONFIG_X86
  3080. {
  3081. /* Need to enable prefetch in the SCSI core for 6400 in x86 */
  3082. u32 prefetch;
  3083. prefetch = readl(&(h->cfgtable->SCSI_Prefetch));
  3084. prefetch |= 0x100;
  3085. writel(prefetch, &(h->cfgtable->SCSI_Prefetch));
  3086. }
  3087. #endif
  3088. /* Disabling DMA prefetch for the P600
  3089. * An ASIC bug may result in a prefetch beyond
  3090. * physical memory.
  3091. */
  3092. if (board_id == 0x3225103C) {
  3093. u32 dma_prefetch;
  3094. dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
  3095. dma_prefetch |= 0x8000;
  3096. writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
  3097. }
  3098. h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
  3099. /* Update the field, and then ring the doorbell */
  3100. writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
  3101. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  3102. /* under certain very rare conditions, this can take awhile.
  3103. * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
  3104. * as we enter this code.)
  3105. */
  3106. for (i = 0; i < MAX_CONFIG_WAIT; i++) {
  3107. if (!(readl(h->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
  3108. break;
  3109. /* delay and try again */
  3110. msleep(10);
  3111. }
  3112. #ifdef HPSA_DEBUG
  3113. print_cfg_table(&pdev->dev, h->cfgtable);
  3114. #endif /* HPSA_DEBUG */
  3115. if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
  3116. dev_warn(&pdev->dev, "unable to get board into simple mode\n");
  3117. err = -ENODEV;
  3118. goto err_out_free_res;
  3119. }
  3120. return 0;
  3121. err_out_free_res:
  3122. /*
  3123. * Deliberately omit pci_disable_device(): it does something nasty to
  3124. * Smart Array controllers that pci_enable_device does not undo
  3125. */
  3126. pci_release_regions(pdev);
  3127. return err;
  3128. }
  3129. static void __devinit hpsa_hba_inquiry(struct ctlr_info *h)
  3130. {
  3131. int rc;
  3132. #define HBA_INQUIRY_BYTE_COUNT 64
  3133. h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
  3134. if (!h->hba_inquiry_data)
  3135. return;
  3136. rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
  3137. h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
  3138. if (rc != 0) {
  3139. kfree(h->hba_inquiry_data);
  3140. h->hba_inquiry_data = NULL;
  3141. }
  3142. }
  3143. static int __devinit hpsa_init_one(struct pci_dev *pdev,
  3144. const struct pci_device_id *ent)
  3145. {
  3146. int i, rc;
  3147. int dac;
  3148. struct ctlr_info *h;
  3149. if (number_of_controllers == 0)
  3150. printk(KERN_INFO DRIVER_NAME "\n");
  3151. if (reset_devices) {
  3152. /* Reset the controller with a PCI power-cycle */
  3153. if (hpsa_hard_reset_controller(pdev) || hpsa_reset_msi(pdev))
  3154. return -ENODEV;
  3155. /* Some devices (notably the HP Smart Array 5i Controller)
  3156. need a little pause here */
  3157. msleep(HPSA_POST_RESET_PAUSE_MSECS);
  3158. /* Now try to get the controller to respond to a no-op */
  3159. for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
  3160. if (hpsa_noop(pdev) == 0)
  3161. break;
  3162. else
  3163. dev_warn(&pdev->dev, "no-op failed%s\n",
  3164. (i < 11 ? "; re-trying" : ""));
  3165. }
  3166. }
  3167. /* Command structures must be aligned on a 32-byte boundary because
  3168. * the 5 lower bits of the address are used by the hardware. and by
  3169. * the driver. See comments in hpsa.h for more info.
  3170. */
  3171. #define COMMANDLIST_ALIGNMENT 32
  3172. BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
  3173. h = kzalloc(sizeof(*h), GFP_KERNEL);
  3174. if (!h)
  3175. return -ENOMEM;
  3176. h->busy_initializing = 1;
  3177. INIT_HLIST_HEAD(&h->cmpQ);
  3178. INIT_HLIST_HEAD(&h->reqQ);
  3179. mutex_init(&h->busy_shutting_down);
  3180. init_completion(&h->scan_wait);
  3181. rc = hpsa_pci_init(h, pdev);
  3182. if (rc != 0)
  3183. goto clean1;
  3184. sprintf(h->devname, "hpsa%d", number_of_controllers);
  3185. h->ctlr = number_of_controllers;
  3186. number_of_controllers++;
  3187. h->pdev = pdev;
  3188. /* configure PCI DMA stuff */
  3189. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  3190. if (rc == 0) {
  3191. dac = 1;
  3192. } else {
  3193. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  3194. if (rc == 0) {
  3195. dac = 0;
  3196. } else {
  3197. dev_err(&pdev->dev, "no suitable DMA available\n");
  3198. goto clean1;
  3199. }
  3200. }
  3201. /* make sure the board interrupts are off */
  3202. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  3203. rc = request_irq(h->intr[PERF_MODE_INT], do_hpsa_intr,
  3204. IRQF_DISABLED, h->devname, h);
  3205. if (rc) {
  3206. dev_err(&pdev->dev, "unable to get irq %d for %s\n",
  3207. h->intr[PERF_MODE_INT], h->devname);
  3208. goto clean2;
  3209. }
  3210. dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
  3211. h->devname, pdev->device,
  3212. h->intr[PERF_MODE_INT], dac ? "" : " not");
  3213. h->cmd_pool_bits =
  3214. kmalloc(((h->nr_cmds + BITS_PER_LONG -
  3215. 1) / BITS_PER_LONG) * sizeof(unsigned long), GFP_KERNEL);
  3216. h->cmd_pool = pci_alloc_consistent(h->pdev,
  3217. h->nr_cmds * sizeof(*h->cmd_pool),
  3218. &(h->cmd_pool_dhandle));
  3219. h->errinfo_pool = pci_alloc_consistent(h->pdev,
  3220. h->nr_cmds * sizeof(*h->errinfo_pool),
  3221. &(h->errinfo_pool_dhandle));
  3222. if ((h->cmd_pool_bits == NULL)
  3223. || (h->cmd_pool == NULL)
  3224. || (h->errinfo_pool == NULL)) {
  3225. dev_err(&pdev->dev, "out of memory");
  3226. rc = -ENOMEM;
  3227. goto clean4;
  3228. }
  3229. spin_lock_init(&h->lock);
  3230. spin_lock_init(&h->scan_lock);
  3231. init_waitqueue_head(&h->scan_wait_queue);
  3232. h->scan_finished = 1; /* no scan currently in progress */
  3233. pci_set_drvdata(pdev, h);
  3234. memset(h->cmd_pool_bits, 0,
  3235. ((h->nr_cmds + BITS_PER_LONG -
  3236. 1) / BITS_PER_LONG) * sizeof(unsigned long));
  3237. hpsa_scsi_setup(h);
  3238. /* Turn the interrupts on so we can service requests */
  3239. h->access.set_intr_mask(h, HPSA_INTR_ON);
  3240. hpsa_put_ctlr_into_performant_mode(h);
  3241. hpsa_hba_inquiry(h);
  3242. hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
  3243. h->busy_initializing = 0;
  3244. return 1;
  3245. clean4:
  3246. kfree(h->cmd_pool_bits);
  3247. if (h->cmd_pool)
  3248. pci_free_consistent(h->pdev,
  3249. h->nr_cmds * sizeof(struct CommandList),
  3250. h->cmd_pool, h->cmd_pool_dhandle);
  3251. if (h->errinfo_pool)
  3252. pci_free_consistent(h->pdev,
  3253. h->nr_cmds * sizeof(struct ErrorInfo),
  3254. h->errinfo_pool,
  3255. h->errinfo_pool_dhandle);
  3256. free_irq(h->intr[PERF_MODE_INT], h);
  3257. clean2:
  3258. clean1:
  3259. h->busy_initializing = 0;
  3260. kfree(h);
  3261. return rc;
  3262. }
  3263. static void hpsa_flush_cache(struct ctlr_info *h)
  3264. {
  3265. char *flush_buf;
  3266. struct CommandList *c;
  3267. flush_buf = kzalloc(4, GFP_KERNEL);
  3268. if (!flush_buf)
  3269. return;
  3270. c = cmd_special_alloc(h);
  3271. if (!c) {
  3272. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  3273. goto out_of_memory;
  3274. }
  3275. fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
  3276. RAID_CTLR_LUNID, TYPE_CMD);
  3277. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
  3278. if (c->err_info->CommandStatus != 0)
  3279. dev_warn(&h->pdev->dev,
  3280. "error flushing cache on controller\n");
  3281. cmd_special_free(h, c);
  3282. out_of_memory:
  3283. kfree(flush_buf);
  3284. }
  3285. static void hpsa_shutdown(struct pci_dev *pdev)
  3286. {
  3287. struct ctlr_info *h;
  3288. h = pci_get_drvdata(pdev);
  3289. /* Turn board interrupts off and send the flush cache command
  3290. * sendcmd will turn off interrupt, and send the flush...
  3291. * To write all data in the battery backed cache to disks
  3292. */
  3293. hpsa_flush_cache(h);
  3294. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  3295. free_irq(h->intr[PERF_MODE_INT], h);
  3296. #ifdef CONFIG_PCI_MSI
  3297. if (h->msix_vector)
  3298. pci_disable_msix(h->pdev);
  3299. else if (h->msi_vector)
  3300. pci_disable_msi(h->pdev);
  3301. #endif /* CONFIG_PCI_MSI */
  3302. }
  3303. static void __devexit hpsa_remove_one(struct pci_dev *pdev)
  3304. {
  3305. struct ctlr_info *h;
  3306. if (pci_get_drvdata(pdev) == NULL) {
  3307. dev_err(&pdev->dev, "unable to remove device \n");
  3308. return;
  3309. }
  3310. h = pci_get_drvdata(pdev);
  3311. mutex_lock(&h->busy_shutting_down);
  3312. remove_from_scan_list(h);
  3313. hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
  3314. hpsa_shutdown(pdev);
  3315. iounmap(h->vaddr);
  3316. pci_free_consistent(h->pdev,
  3317. h->nr_cmds * sizeof(struct CommandList),
  3318. h->cmd_pool, h->cmd_pool_dhandle);
  3319. pci_free_consistent(h->pdev,
  3320. h->nr_cmds * sizeof(struct ErrorInfo),
  3321. h->errinfo_pool, h->errinfo_pool_dhandle);
  3322. pci_free_consistent(h->pdev, h->reply_pool_size,
  3323. h->reply_pool, h->reply_pool_dhandle);
  3324. kfree(h->cmd_pool_bits);
  3325. kfree(h->blockFetchTable);
  3326. kfree(h->hba_inquiry_data);
  3327. /*
  3328. * Deliberately omit pci_disable_device(): it does something nasty to
  3329. * Smart Array controllers that pci_enable_device does not undo
  3330. */
  3331. pci_release_regions(pdev);
  3332. pci_set_drvdata(pdev, NULL);
  3333. mutex_unlock(&h->busy_shutting_down);
  3334. kfree(h);
  3335. }
  3336. static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
  3337. __attribute__((unused)) pm_message_t state)
  3338. {
  3339. return -ENOSYS;
  3340. }
  3341. static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
  3342. {
  3343. return -ENOSYS;
  3344. }
  3345. static struct pci_driver hpsa_pci_driver = {
  3346. .name = "hpsa",
  3347. .probe = hpsa_init_one,
  3348. .remove = __devexit_p(hpsa_remove_one),
  3349. .id_table = hpsa_pci_device_id, /* id_table */
  3350. .shutdown = hpsa_shutdown,
  3351. .suspend = hpsa_suspend,
  3352. .resume = hpsa_resume,
  3353. };
  3354. /* Fill in bucket_map[], given nsgs (the max number of
  3355. * scatter gather elements supported) and bucket[],
  3356. * which is an array of 8 integers. The bucket[] array
  3357. * contains 8 different DMA transfer sizes (in 16
  3358. * byte increments) which the controller uses to fetch
  3359. * commands. This function fills in bucket_map[], which
  3360. * maps a given number of scatter gather elements to one of
  3361. * the 8 DMA transfer sizes. The point of it is to allow the
  3362. * controller to only do as much DMA as needed to fetch the
  3363. * command, with the DMA transfer size encoded in the lower
  3364. * bits of the command address.
  3365. */
  3366. static void calc_bucket_map(int bucket[], int num_buckets,
  3367. int nsgs, int *bucket_map)
  3368. {
  3369. int i, j, b, size;
  3370. /* even a command with 0 SGs requires 4 blocks */
  3371. #define MINIMUM_TRANSFER_BLOCKS 4
  3372. #define NUM_BUCKETS 8
  3373. /* Note, bucket_map must have nsgs+1 entries. */
  3374. for (i = 0; i <= nsgs; i++) {
  3375. /* Compute size of a command with i SG entries */
  3376. size = i + MINIMUM_TRANSFER_BLOCKS;
  3377. b = num_buckets; /* Assume the biggest bucket */
  3378. /* Find the bucket that is just big enough */
  3379. for (j = 0; j < 8; j++) {
  3380. if (bucket[j] >= size) {
  3381. b = j;
  3382. break;
  3383. }
  3384. }
  3385. /* for a command with i SG entries, use bucket b. */
  3386. bucket_map[i] = b;
  3387. }
  3388. }
  3389. static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
  3390. {
  3391. u32 trans_support;
  3392. u64 trans_offset;
  3393. /* 5 = 1 s/g entry or 4k
  3394. * 6 = 2 s/g entry or 8k
  3395. * 8 = 4 s/g entry or 16k
  3396. * 10 = 6 s/g entry or 24k
  3397. */
  3398. int bft[8] = {5, 6, 8, 10, 12, 20, 28, 35}; /* for scatter/gathers */
  3399. int i = 0;
  3400. int l = 0;
  3401. unsigned long register_value;
  3402. trans_support = readl(&(h->cfgtable->TransportSupport));
  3403. if (!(trans_support & PERFORMANT_MODE))
  3404. return;
  3405. h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
  3406. h->max_sg_entries = 32;
  3407. /* Performant mode ring buffer and supporting data structures */
  3408. h->reply_pool_size = h->max_commands * sizeof(u64);
  3409. h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
  3410. &(h->reply_pool_dhandle));
  3411. /* Need a block fetch table for performant mode */
  3412. h->blockFetchTable = kmalloc(((h->max_sg_entries+1) *
  3413. sizeof(u32)), GFP_KERNEL);
  3414. if ((h->reply_pool == NULL)
  3415. || (h->blockFetchTable == NULL))
  3416. goto clean_up;
  3417. h->reply_pool_wraparound = 1; /* spec: init to 1 */
  3418. /* Controller spec: zero out this buffer. */
  3419. memset(h->reply_pool, 0, h->reply_pool_size);
  3420. h->reply_pool_head = h->reply_pool;
  3421. trans_offset = readl(&(h->cfgtable->TransMethodOffset));
  3422. bft[7] = h->max_sg_entries + 4;
  3423. calc_bucket_map(bft, ARRAY_SIZE(bft), 32, h->blockFetchTable);
  3424. for (i = 0; i < 8; i++)
  3425. writel(bft[i], &h->transtable->BlockFetch[i]);
  3426. /* size of controller ring buffer */
  3427. writel(h->max_commands, &h->transtable->RepQSize);
  3428. writel(1, &h->transtable->RepQCount);
  3429. writel(0, &h->transtable->RepQCtrAddrLow32);
  3430. writel(0, &h->transtable->RepQCtrAddrHigh32);
  3431. writel(h->reply_pool_dhandle, &h->transtable->RepQAddr0Low32);
  3432. writel(0, &h->transtable->RepQAddr0High32);
  3433. writel(CFGTBL_Trans_Performant,
  3434. &(h->cfgtable->HostWrite.TransportRequest));
  3435. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  3436. /* under certain very rare conditions, this can take awhile.
  3437. * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
  3438. * as we enter this code.) */
  3439. for (l = 0; l < MAX_CONFIG_WAIT; l++) {
  3440. register_value = readl(h->vaddr + SA5_DOORBELL);
  3441. if (!(register_value & CFGTBL_ChangeReq))
  3442. break;
  3443. /* delay and try again */
  3444. set_current_state(TASK_INTERRUPTIBLE);
  3445. schedule_timeout(10);
  3446. }
  3447. register_value = readl(&(h->cfgtable->TransportActive));
  3448. if (!(register_value & CFGTBL_Trans_Performant)) {
  3449. dev_warn(&h->pdev->dev, "unable to get board into"
  3450. " performant mode\n");
  3451. return;
  3452. }
  3453. /* Change the access methods to the performant access methods */
  3454. h->access = SA5_performant_access;
  3455. h->transMethod = CFGTBL_Trans_Performant;
  3456. return;
  3457. clean_up:
  3458. if (h->reply_pool)
  3459. pci_free_consistent(h->pdev, h->reply_pool_size,
  3460. h->reply_pool, h->reply_pool_dhandle);
  3461. kfree(h->blockFetchTable);
  3462. }
  3463. /*
  3464. * This is it. Register the PCI driver information for the cards we control
  3465. * the OS will call our registered routines when it finds one of our cards.
  3466. */
  3467. static int __init hpsa_init(void)
  3468. {
  3469. int err;
  3470. /* Start the scan thread */
  3471. hpsa_scan_thread = kthread_run(hpsa_scan_func, NULL, "hpsa_scan");
  3472. if (IS_ERR(hpsa_scan_thread)) {
  3473. err = PTR_ERR(hpsa_scan_thread);
  3474. return -ENODEV;
  3475. }
  3476. err = pci_register_driver(&hpsa_pci_driver);
  3477. if (err)
  3478. kthread_stop(hpsa_scan_thread);
  3479. return err;
  3480. }
  3481. static void __exit hpsa_cleanup(void)
  3482. {
  3483. pci_unregister_driver(&hpsa_pci_driver);
  3484. kthread_stop(hpsa_scan_thread);
  3485. }
  3486. module_init(hpsa_init);
  3487. module_exit(hpsa_cleanup);