hpsa.c 105 KB

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