qla_os.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2005 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include <linux/moduleparam.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/delay.h>
  11. #include <linux/kthread.h>
  12. #include <scsi/scsi_tcq.h>
  13. #include <scsi/scsicam.h>
  14. #include <scsi/scsi_transport.h>
  15. #include <scsi/scsi_transport_fc.h>
  16. /*
  17. * Driver version
  18. */
  19. char qla2x00_version_str[40];
  20. /*
  21. * SRB allocation cache
  22. */
  23. static kmem_cache_t *srb_cachep;
  24. /*
  25. * Ioctl related information.
  26. */
  27. static int num_hosts;
  28. int ql2xlogintimeout = 20;
  29. module_param(ql2xlogintimeout, int, S_IRUGO|S_IRUSR);
  30. MODULE_PARM_DESC(ql2xlogintimeout,
  31. "Login timeout value in seconds.");
  32. int qlport_down_retry = 30;
  33. module_param(qlport_down_retry, int, S_IRUGO|S_IRUSR);
  34. MODULE_PARM_DESC(qlport_down_retry,
  35. "Maximum number of command retries to a port that returns "
  36. "a PORT-DOWN status.");
  37. int ql2xplogiabsentdevice;
  38. module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
  39. MODULE_PARM_DESC(ql2xplogiabsentdevice,
  40. "Option to enable PLOGI to devices that are not present after "
  41. "a Fabric scan. This is needed for several broken switches. "
  42. "Default is 0 - no PLOGI. 1 - perfom PLOGI.");
  43. int ql2xloginretrycount = 0;
  44. module_param(ql2xloginretrycount, int, S_IRUGO|S_IRUSR);
  45. MODULE_PARM_DESC(ql2xloginretrycount,
  46. "Specify an alternate value for the NVRAM login retry count.");
  47. int ql2xallocfwdump = 1;
  48. module_param(ql2xallocfwdump, int, S_IRUGO|S_IRUSR);
  49. MODULE_PARM_DESC(ql2xallocfwdump,
  50. "Option to enable allocation of memory for a firmware dump "
  51. "during HBA initialization. Memory allocation requirements "
  52. "vary by ISP type. Default is 1 - allocate memory.");
  53. int ql2xextended_error_logging;
  54. module_param(ql2xextended_error_logging, int, S_IRUGO|S_IRUSR);
  55. MODULE_PARM_DESC(ql2xextended_error_logging,
  56. "Option to enable extended error logging, "
  57. "Default is 0 - no logging. 1 - log errors.");
  58. static void qla2x00_free_device(scsi_qla_host_t *);
  59. static void qla2x00_config_dma_addressing(scsi_qla_host_t *ha);
  60. int ql2xfdmienable;
  61. module_param(ql2xfdmienable, int, S_IRUGO|S_IRUSR);
  62. MODULE_PARM_DESC(ql2xfdmienable,
  63. "Enables FDMI registratons "
  64. "Default is 0 - no FDMI. 1 - perfom FDMI.");
  65. #define MAX_Q_DEPTH 32
  66. static int ql2xmaxqdepth = MAX_Q_DEPTH;
  67. module_param(ql2xmaxqdepth, int, S_IRUGO|S_IWUSR);
  68. MODULE_PARM_DESC(ql2xmaxqdepth,
  69. "Maximum queue depth to report for target devices.");
  70. int ql2xqfullrampup = 120;
  71. module_param(ql2xqfullrampup, int, S_IRUGO|S_IWUSR);
  72. MODULE_PARM_DESC(ql2xqfullrampup,
  73. "Number of seconds to wait to begin to ramp-up the queue "
  74. "depth for a device after a queue-full condition has been "
  75. "detected. Default is 120 seconds.");
  76. /*
  77. * SCSI host template entry points
  78. */
  79. static int qla2xxx_slave_configure(struct scsi_device * device);
  80. static int qla2xxx_slave_alloc(struct scsi_device *);
  81. static void qla2xxx_slave_destroy(struct scsi_device *);
  82. static int qla2x00_queuecommand(struct scsi_cmnd *cmd,
  83. void (*fn)(struct scsi_cmnd *));
  84. static int qla24xx_queuecommand(struct scsi_cmnd *cmd,
  85. void (*fn)(struct scsi_cmnd *));
  86. static int qla2xxx_eh_abort(struct scsi_cmnd *);
  87. static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
  88. static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
  89. static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
  90. static int qla2x00_loop_reset(scsi_qla_host_t *ha);
  91. static int qla2x00_device_reset(scsi_qla_host_t *, fc_port_t *);
  92. static int qla2x00_change_queue_depth(struct scsi_device *, int);
  93. static int qla2x00_change_queue_type(struct scsi_device *, int);
  94. static struct scsi_host_template qla2x00_driver_template = {
  95. .module = THIS_MODULE,
  96. .name = QLA2XXX_DRIVER_NAME,
  97. .queuecommand = qla2x00_queuecommand,
  98. .eh_abort_handler = qla2xxx_eh_abort,
  99. .eh_device_reset_handler = qla2xxx_eh_device_reset,
  100. .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
  101. .eh_host_reset_handler = qla2xxx_eh_host_reset,
  102. .slave_configure = qla2xxx_slave_configure,
  103. .slave_alloc = qla2xxx_slave_alloc,
  104. .slave_destroy = qla2xxx_slave_destroy,
  105. .change_queue_depth = qla2x00_change_queue_depth,
  106. .change_queue_type = qla2x00_change_queue_type,
  107. .this_id = -1,
  108. .cmd_per_lun = 3,
  109. .use_clustering = ENABLE_CLUSTERING,
  110. .sg_tablesize = SG_ALL,
  111. /*
  112. * The RISC allows for each command to transfer (2^32-1) bytes of data,
  113. * which equates to 0x800000 sectors.
  114. */
  115. .max_sectors = 0xFFFF,
  116. .shost_attrs = qla2x00_host_attrs,
  117. };
  118. static struct scsi_host_template qla24xx_driver_template = {
  119. .module = THIS_MODULE,
  120. .name = QLA2XXX_DRIVER_NAME,
  121. .queuecommand = qla24xx_queuecommand,
  122. .eh_abort_handler = qla2xxx_eh_abort,
  123. .eh_device_reset_handler = qla2xxx_eh_device_reset,
  124. .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
  125. .eh_host_reset_handler = qla2xxx_eh_host_reset,
  126. .slave_configure = qla2xxx_slave_configure,
  127. .slave_alloc = qla2xxx_slave_alloc,
  128. .slave_destroy = qla2xxx_slave_destroy,
  129. .change_queue_depth = qla2x00_change_queue_depth,
  130. .change_queue_type = qla2x00_change_queue_type,
  131. .this_id = -1,
  132. .cmd_per_lun = 3,
  133. .use_clustering = ENABLE_CLUSTERING,
  134. .sg_tablesize = SG_ALL,
  135. .max_sectors = 0xFFFF,
  136. .shost_attrs = qla2x00_host_attrs,
  137. };
  138. static struct scsi_transport_template *qla2xxx_transport_template = NULL;
  139. /* TODO Convert to inlines
  140. *
  141. * Timer routines
  142. */
  143. #define WATCH_INTERVAL 1 /* number of seconds */
  144. static void qla2x00_timer(scsi_qla_host_t *);
  145. static __inline__ void qla2x00_start_timer(scsi_qla_host_t *,
  146. void *, unsigned long);
  147. static __inline__ void qla2x00_restart_timer(scsi_qla_host_t *, unsigned long);
  148. static __inline__ void qla2x00_stop_timer(scsi_qla_host_t *);
  149. static inline void
  150. qla2x00_start_timer(scsi_qla_host_t *ha, void *func, unsigned long interval)
  151. {
  152. init_timer(&ha->timer);
  153. ha->timer.expires = jiffies + interval * HZ;
  154. ha->timer.data = (unsigned long)ha;
  155. ha->timer.function = (void (*)(unsigned long))func;
  156. add_timer(&ha->timer);
  157. ha->timer_active = 1;
  158. }
  159. static inline void
  160. qla2x00_restart_timer(scsi_qla_host_t *ha, unsigned long interval)
  161. {
  162. mod_timer(&ha->timer, jiffies + interval * HZ);
  163. }
  164. static __inline__ void
  165. qla2x00_stop_timer(scsi_qla_host_t *ha)
  166. {
  167. del_timer_sync(&ha->timer);
  168. ha->timer_active = 0;
  169. }
  170. static int qla2x00_do_dpc(void *data);
  171. static void qla2x00_rst_aen(scsi_qla_host_t *);
  172. static uint8_t qla2x00_mem_alloc(scsi_qla_host_t *);
  173. static void qla2x00_mem_free(scsi_qla_host_t *ha);
  174. static int qla2x00_allocate_sp_pool( scsi_qla_host_t *ha);
  175. static void qla2x00_free_sp_pool(scsi_qla_host_t *ha);
  176. static void qla2x00_sp_free_dma(scsi_qla_host_t *, srb_t *);
  177. void qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *);
  178. /* -------------------------------------------------------------------------- */
  179. static char *
  180. qla2x00_pci_info_str(struct scsi_qla_host *ha, char *str)
  181. {
  182. static char *pci_bus_modes[] = {
  183. "33", "66", "100", "133",
  184. };
  185. uint16_t pci_bus;
  186. strcpy(str, "PCI");
  187. pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
  188. if (pci_bus) {
  189. strcat(str, "-X (");
  190. strcat(str, pci_bus_modes[pci_bus]);
  191. } else {
  192. pci_bus = (ha->pci_attr & BIT_8) >> 8;
  193. strcat(str, " (");
  194. strcat(str, pci_bus_modes[pci_bus]);
  195. }
  196. strcat(str, " MHz)");
  197. return (str);
  198. }
  199. static char *
  200. qla24xx_pci_info_str(struct scsi_qla_host *ha, char *str)
  201. {
  202. static char *pci_bus_modes[] = { "33", "66", "100", "133", };
  203. uint32_t pci_bus;
  204. int pcie_reg;
  205. pcie_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
  206. if (pcie_reg) {
  207. char lwstr[6];
  208. uint16_t pcie_lstat, lspeed, lwidth;
  209. pcie_reg += 0x12;
  210. pci_read_config_word(ha->pdev, pcie_reg, &pcie_lstat);
  211. lspeed = pcie_lstat & (BIT_0 | BIT_1 | BIT_2 | BIT_3);
  212. lwidth = (pcie_lstat &
  213. (BIT_4 | BIT_5 | BIT_6 | BIT_7 | BIT_8 | BIT_9)) >> 4;
  214. strcpy(str, "PCIe (");
  215. if (lspeed == 1)
  216. strcat(str, "2.5Gb/s ");
  217. else
  218. strcat(str, "<unknown> ");
  219. snprintf(lwstr, sizeof(lwstr), "x%d)", lwidth);
  220. strcat(str, lwstr);
  221. return str;
  222. }
  223. strcpy(str, "PCI");
  224. pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
  225. if (pci_bus == 0 || pci_bus == 8) {
  226. strcat(str, " (");
  227. strcat(str, pci_bus_modes[pci_bus >> 3]);
  228. } else {
  229. strcat(str, "-X ");
  230. if (pci_bus & BIT_2)
  231. strcat(str, "Mode 2");
  232. else
  233. strcat(str, "Mode 1");
  234. strcat(str, " (");
  235. strcat(str, pci_bus_modes[pci_bus & ~BIT_2]);
  236. }
  237. strcat(str, " MHz)");
  238. return str;
  239. }
  240. static char *
  241. qla2x00_fw_version_str(struct scsi_qla_host *ha, char *str)
  242. {
  243. char un_str[10];
  244. sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
  245. ha->fw_minor_version,
  246. ha->fw_subminor_version);
  247. if (ha->fw_attributes & BIT_9) {
  248. strcat(str, "FLX");
  249. return (str);
  250. }
  251. switch (ha->fw_attributes & 0xFF) {
  252. case 0x7:
  253. strcat(str, "EF");
  254. break;
  255. case 0x17:
  256. strcat(str, "TP");
  257. break;
  258. case 0x37:
  259. strcat(str, "IP");
  260. break;
  261. case 0x77:
  262. strcat(str, "VI");
  263. break;
  264. default:
  265. sprintf(un_str, "(%x)", ha->fw_attributes);
  266. strcat(str, un_str);
  267. break;
  268. }
  269. if (ha->fw_attributes & 0x100)
  270. strcat(str, "X");
  271. return (str);
  272. }
  273. static char *
  274. qla24xx_fw_version_str(struct scsi_qla_host *ha, char *str)
  275. {
  276. sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
  277. ha->fw_minor_version,
  278. ha->fw_subminor_version);
  279. if (ha->fw_attributes & BIT_0)
  280. strcat(str, "[Class 2] ");
  281. if (ha->fw_attributes & BIT_1)
  282. strcat(str, "[IP] ");
  283. if (ha->fw_attributes & BIT_2)
  284. strcat(str, "[Multi-ID] ");
  285. if (ha->fw_attributes & BIT_13)
  286. strcat(str, "[Experimental]");
  287. return str;
  288. }
  289. static inline srb_t *
  290. qla2x00_get_new_sp(scsi_qla_host_t *ha, fc_port_t *fcport,
  291. struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  292. {
  293. srb_t *sp;
  294. sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
  295. if (!sp)
  296. return sp;
  297. sp->ha = ha;
  298. sp->fcport = fcport;
  299. sp->cmd = cmd;
  300. sp->flags = 0;
  301. CMD_SP(cmd) = (void *)sp;
  302. cmd->scsi_done = done;
  303. return sp;
  304. }
  305. static int
  306. qla2x00_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  307. {
  308. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  309. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  310. struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
  311. srb_t *sp;
  312. int rval;
  313. rval = fc_remote_port_chkready(rport);
  314. if (rval) {
  315. cmd->result = rval;
  316. goto qc_fail_command;
  317. }
  318. /* Close window on fcport/rport state-transitioning. */
  319. if (!*(fc_port_t **)rport->dd_data) {
  320. cmd->result = DID_IMM_RETRY << 16;
  321. goto qc_fail_command;
  322. }
  323. if (atomic_read(&fcport->state) != FCS_ONLINE) {
  324. if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
  325. atomic_read(&ha->loop_state) == LOOP_DEAD) {
  326. cmd->result = DID_NO_CONNECT << 16;
  327. goto qc_fail_command;
  328. }
  329. goto qc_host_busy;
  330. }
  331. spin_unlock_irq(ha->host->host_lock);
  332. sp = qla2x00_get_new_sp(ha, fcport, cmd, done);
  333. if (!sp)
  334. goto qc_host_busy_lock;
  335. rval = qla2x00_start_scsi(sp);
  336. if (rval != QLA_SUCCESS)
  337. goto qc_host_busy_free_sp;
  338. spin_lock_irq(ha->host->host_lock);
  339. return 0;
  340. qc_host_busy_free_sp:
  341. qla2x00_sp_free_dma(ha, sp);
  342. mempool_free(sp, ha->srb_mempool);
  343. qc_host_busy_lock:
  344. spin_lock_irq(ha->host->host_lock);
  345. qc_host_busy:
  346. return SCSI_MLQUEUE_HOST_BUSY;
  347. qc_fail_command:
  348. done(cmd);
  349. return 0;
  350. }
  351. static int
  352. qla24xx_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  353. {
  354. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  355. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  356. struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
  357. srb_t *sp;
  358. int rval;
  359. rval = fc_remote_port_chkready(rport);
  360. if (rval) {
  361. cmd->result = rval;
  362. goto qc24_fail_command;
  363. }
  364. /* Close window on fcport/rport state-transitioning. */
  365. if (!*(fc_port_t **)rport->dd_data) {
  366. cmd->result = DID_IMM_RETRY << 16;
  367. goto qc24_fail_command;
  368. }
  369. if (atomic_read(&fcport->state) != FCS_ONLINE) {
  370. if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
  371. atomic_read(&ha->loop_state) == LOOP_DEAD) {
  372. cmd->result = DID_NO_CONNECT << 16;
  373. goto qc24_fail_command;
  374. }
  375. goto qc24_host_busy;
  376. }
  377. spin_unlock_irq(ha->host->host_lock);
  378. sp = qla2x00_get_new_sp(ha, fcport, cmd, done);
  379. if (!sp)
  380. goto qc24_host_busy_lock;
  381. rval = qla24xx_start_scsi(sp);
  382. if (rval != QLA_SUCCESS)
  383. goto qc24_host_busy_free_sp;
  384. spin_lock_irq(ha->host->host_lock);
  385. return 0;
  386. qc24_host_busy_free_sp:
  387. qla2x00_sp_free_dma(ha, sp);
  388. mempool_free(sp, ha->srb_mempool);
  389. qc24_host_busy_lock:
  390. spin_lock_irq(ha->host->host_lock);
  391. qc24_host_busy:
  392. return SCSI_MLQUEUE_HOST_BUSY;
  393. qc24_fail_command:
  394. done(cmd);
  395. return 0;
  396. }
  397. /*
  398. * qla2x00_eh_wait_on_command
  399. * Waits for the command to be returned by the Firmware for some
  400. * max time.
  401. *
  402. * Input:
  403. * ha = actual ha whose done queue will contain the command
  404. * returned by firmware.
  405. * cmd = Scsi Command to wait on.
  406. * flag = Abort/Reset(Bus or Device Reset)
  407. *
  408. * Return:
  409. * Not Found : 0
  410. * Found : 1
  411. */
  412. static int
  413. qla2x00_eh_wait_on_command(scsi_qla_host_t *ha, struct scsi_cmnd *cmd)
  414. {
  415. #define ABORT_POLLING_PERIOD 1000
  416. #define ABORT_WAIT_ITER ((10 * 1000) / (ABORT_POLLING_PERIOD))
  417. unsigned long wait_iter = ABORT_WAIT_ITER;
  418. int ret = QLA_SUCCESS;
  419. while (CMD_SP(cmd)) {
  420. msleep(ABORT_POLLING_PERIOD);
  421. if (--wait_iter)
  422. break;
  423. }
  424. if (CMD_SP(cmd))
  425. ret = QLA_FUNCTION_FAILED;
  426. return ret;
  427. }
  428. /*
  429. * qla2x00_wait_for_hba_online
  430. * Wait till the HBA is online after going through
  431. * <= MAX_RETRIES_OF_ISP_ABORT or
  432. * finally HBA is disabled ie marked offline
  433. *
  434. * Input:
  435. * ha - pointer to host adapter structure
  436. *
  437. * Note:
  438. * Does context switching-Release SPIN_LOCK
  439. * (if any) before calling this routine.
  440. *
  441. * Return:
  442. * Success (Adapter is online) : 0
  443. * Failed (Adapter is offline/disabled) : 1
  444. */
  445. int
  446. qla2x00_wait_for_hba_online(scsi_qla_host_t *ha)
  447. {
  448. int return_status;
  449. unsigned long wait_online;
  450. wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
  451. while (((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) ||
  452. test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
  453. test_bit(ISP_ABORT_RETRY, &ha->dpc_flags) ||
  454. ha->dpc_active) && time_before(jiffies, wait_online)) {
  455. msleep(1000);
  456. }
  457. if (ha->flags.online)
  458. return_status = QLA_SUCCESS;
  459. else
  460. return_status = QLA_FUNCTION_FAILED;
  461. DEBUG2(printk("%s return_status=%d\n",__func__,return_status));
  462. return (return_status);
  463. }
  464. /*
  465. * qla2x00_wait_for_loop_ready
  466. * Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
  467. * to be in LOOP_READY state.
  468. * Input:
  469. * ha - pointer to host adapter structure
  470. *
  471. * Note:
  472. * Does context switching-Release SPIN_LOCK
  473. * (if any) before calling this routine.
  474. *
  475. *
  476. * Return:
  477. * Success (LOOP_READY) : 0
  478. * Failed (LOOP_NOT_READY) : 1
  479. */
  480. static inline int
  481. qla2x00_wait_for_loop_ready(scsi_qla_host_t *ha)
  482. {
  483. int return_status = QLA_SUCCESS;
  484. unsigned long loop_timeout ;
  485. /* wait for 5 min at the max for loop to be ready */
  486. loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ);
  487. while ((!atomic_read(&ha->loop_down_timer) &&
  488. atomic_read(&ha->loop_state) == LOOP_DOWN) ||
  489. atomic_read(&ha->loop_state) != LOOP_READY) {
  490. if (atomic_read(&ha->loop_state) == LOOP_DEAD) {
  491. return_status = QLA_FUNCTION_FAILED;
  492. break;
  493. }
  494. msleep(1000);
  495. if (time_after_eq(jiffies, loop_timeout)) {
  496. return_status = QLA_FUNCTION_FAILED;
  497. break;
  498. }
  499. }
  500. return (return_status);
  501. }
  502. static void
  503. qla2x00_block_error_handler(struct scsi_cmnd *cmnd)
  504. {
  505. struct Scsi_Host *shost = cmnd->device->host;
  506. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  507. unsigned long flags;
  508. spin_lock_irqsave(shost->host_lock, flags);
  509. while (rport->port_state == FC_PORTSTATE_BLOCKED) {
  510. spin_unlock_irqrestore(shost->host_lock, flags);
  511. msleep(1000);
  512. spin_lock_irqsave(shost->host_lock, flags);
  513. }
  514. spin_unlock_irqrestore(shost->host_lock, flags);
  515. return;
  516. }
  517. /**************************************************************************
  518. * qla2xxx_eh_abort
  519. *
  520. * Description:
  521. * The abort function will abort the specified command.
  522. *
  523. * Input:
  524. * cmd = Linux SCSI command packet to be aborted.
  525. *
  526. * Returns:
  527. * Either SUCCESS or FAILED.
  528. *
  529. * Note:
  530. * Only return FAILED if command not returned by firmware.
  531. **************************************************************************/
  532. static int
  533. qla2xxx_eh_abort(struct scsi_cmnd *cmd)
  534. {
  535. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  536. srb_t *sp;
  537. int ret, i;
  538. unsigned int id, lun;
  539. unsigned long serial;
  540. unsigned long flags;
  541. int wait = 0;
  542. qla2x00_block_error_handler(cmd);
  543. if (!CMD_SP(cmd))
  544. return SUCCESS;
  545. ret = SUCCESS;
  546. id = cmd->device->id;
  547. lun = cmd->device->lun;
  548. serial = cmd->serial_number;
  549. /* Check active list for command command. */
  550. spin_lock_irqsave(&ha->hardware_lock, flags);
  551. for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
  552. sp = ha->outstanding_cmds[i];
  553. if (sp == NULL)
  554. continue;
  555. if (sp->cmd != cmd)
  556. continue;
  557. DEBUG2(printk("%s(%ld): aborting sp %p from RISC. pid=%ld.\n",
  558. __func__, ha->host_no, sp, serial));
  559. DEBUG3(qla2x00_print_scsi_cmd(cmd));
  560. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  561. if (ha->isp_ops.abort_command(ha, sp)) {
  562. DEBUG2(printk("%s(%ld): abort_command "
  563. "mbx failed.\n", __func__, ha->host_no));
  564. } else {
  565. DEBUG3(printk("%s(%ld): abort_command "
  566. "mbx success.\n", __func__, ha->host_no));
  567. wait = 1;
  568. }
  569. spin_lock_irqsave(&ha->hardware_lock, flags);
  570. break;
  571. }
  572. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  573. /* Wait for the command to be returned. */
  574. if (wait) {
  575. if (qla2x00_eh_wait_on_command(ha, cmd) != QLA_SUCCESS) {
  576. qla_printk(KERN_ERR, ha,
  577. "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
  578. "%x.\n", ha->host_no, id, lun, serial, ret);
  579. ret = FAILED;
  580. }
  581. }
  582. qla_printk(KERN_INFO, ha,
  583. "scsi(%ld:%d:%d): Abort command issued -- %d %lx %x.\n",
  584. ha->host_no, id, lun, wait, serial, ret);
  585. return ret;
  586. }
  587. /**************************************************************************
  588. * qla2x00_eh_wait_for_pending_target_commands
  589. *
  590. * Description:
  591. * Waits for all the commands to come back from the specified target.
  592. *
  593. * Input:
  594. * ha - pointer to scsi_qla_host structure.
  595. * t - target
  596. * Returns:
  597. * Either SUCCESS or FAILED.
  598. *
  599. * Note:
  600. **************************************************************************/
  601. static int
  602. qla2x00_eh_wait_for_pending_target_commands(scsi_qla_host_t *ha, unsigned int t)
  603. {
  604. int cnt;
  605. int status;
  606. srb_t *sp;
  607. struct scsi_cmnd *cmd;
  608. unsigned long flags;
  609. status = 0;
  610. /*
  611. * Waiting for all commands for the designated target in the active
  612. * array
  613. */
  614. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  615. spin_lock_irqsave(&ha->hardware_lock, flags);
  616. sp = ha->outstanding_cmds[cnt];
  617. if (sp) {
  618. cmd = sp->cmd;
  619. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  620. if (cmd->device->id == t) {
  621. if (!qla2x00_eh_wait_on_command(ha, cmd)) {
  622. status = 1;
  623. break;
  624. }
  625. }
  626. } else {
  627. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  628. }
  629. }
  630. return (status);
  631. }
  632. /**************************************************************************
  633. * qla2xxx_eh_device_reset
  634. *
  635. * Description:
  636. * The device reset function will reset the target and abort any
  637. * executing commands.
  638. *
  639. * NOTE: The use of SP is undefined within this context. Do *NOT*
  640. * attempt to use this value, even if you determine it is
  641. * non-null.
  642. *
  643. * Input:
  644. * cmd = Linux SCSI command packet of the command that cause the
  645. * bus device reset.
  646. *
  647. * Returns:
  648. * SUCCESS/FAILURE (defined as macro in scsi.h).
  649. *
  650. **************************************************************************/
  651. static int
  652. qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
  653. {
  654. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  655. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  656. int ret;
  657. unsigned int id, lun;
  658. unsigned long serial;
  659. qla2x00_block_error_handler(cmd);
  660. ret = FAILED;
  661. id = cmd->device->id;
  662. lun = cmd->device->lun;
  663. serial = cmd->serial_number;
  664. if (!fcport)
  665. return ret;
  666. qla_printk(KERN_INFO, ha,
  667. "scsi(%ld:%d:%d): DEVICE RESET ISSUED.\n", ha->host_no, id, lun);
  668. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  669. goto eh_dev_reset_done;
  670. if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
  671. if (qla2x00_device_reset(ha, fcport) == 0)
  672. ret = SUCCESS;
  673. #if defined(LOGOUT_AFTER_DEVICE_RESET)
  674. if (ret == SUCCESS) {
  675. if (fcport->flags & FC_FABRIC_DEVICE) {
  676. ha->isp_ops.fabric_logout(ha, fcport->loop_id);
  677. qla2x00_mark_device_lost(ha, fcport, 0, 0);
  678. }
  679. }
  680. #endif
  681. } else {
  682. DEBUG2(printk(KERN_INFO
  683. "%s failed: loop not ready\n",__func__));
  684. }
  685. if (ret == FAILED) {
  686. DEBUG3(printk("%s(%ld): device reset failed\n",
  687. __func__, ha->host_no));
  688. qla_printk(KERN_INFO, ha, "%s: device reset failed\n",
  689. __func__);
  690. goto eh_dev_reset_done;
  691. }
  692. /* Flush outstanding commands. */
  693. if (qla2x00_eh_wait_for_pending_target_commands(ha, id))
  694. ret = FAILED;
  695. if (ret == FAILED) {
  696. DEBUG3(printk("%s(%ld): failed while waiting for commands\n",
  697. __func__, ha->host_no));
  698. qla_printk(KERN_INFO, ha,
  699. "%s: failed while waiting for commands\n", __func__);
  700. } else
  701. qla_printk(KERN_INFO, ha,
  702. "scsi(%ld:%d:%d): DEVICE RESET SUCCEEDED.\n", ha->host_no,
  703. id, lun);
  704. eh_dev_reset_done:
  705. return ret;
  706. }
  707. /**************************************************************************
  708. * qla2x00_eh_wait_for_pending_commands
  709. *
  710. * Description:
  711. * Waits for all the commands to come back from the specified host.
  712. *
  713. * Input:
  714. * ha - pointer to scsi_qla_host structure.
  715. *
  716. * Returns:
  717. * 1 : SUCCESS
  718. * 0 : FAILED
  719. *
  720. * Note:
  721. **************************************************************************/
  722. static int
  723. qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *ha)
  724. {
  725. int cnt;
  726. int status;
  727. srb_t *sp;
  728. struct scsi_cmnd *cmd;
  729. unsigned long flags;
  730. status = 1;
  731. /*
  732. * Waiting for all commands for the designated target in the active
  733. * array
  734. */
  735. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  736. spin_lock_irqsave(&ha->hardware_lock, flags);
  737. sp = ha->outstanding_cmds[cnt];
  738. if (sp) {
  739. cmd = sp->cmd;
  740. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  741. status = qla2x00_eh_wait_on_command(ha, cmd);
  742. if (status == 0)
  743. break;
  744. }
  745. else {
  746. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  747. }
  748. }
  749. return (status);
  750. }
  751. /**************************************************************************
  752. * qla2xxx_eh_bus_reset
  753. *
  754. * Description:
  755. * The bus reset function will reset the bus and abort any executing
  756. * commands.
  757. *
  758. * Input:
  759. * cmd = Linux SCSI command packet of the command that cause the
  760. * bus reset.
  761. *
  762. * Returns:
  763. * SUCCESS/FAILURE (defined as macro in scsi.h).
  764. *
  765. **************************************************************************/
  766. static int
  767. qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
  768. {
  769. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  770. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  771. int ret;
  772. unsigned int id, lun;
  773. unsigned long serial;
  774. qla2x00_block_error_handler(cmd);
  775. ret = FAILED;
  776. id = cmd->device->id;
  777. lun = cmd->device->lun;
  778. serial = cmd->serial_number;
  779. if (!fcport)
  780. return ret;
  781. qla_printk(KERN_INFO, ha,
  782. "scsi(%ld:%d:%d): LOOP RESET ISSUED.\n", ha->host_no, id, lun);
  783. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
  784. DEBUG2(printk("%s failed:board disabled\n",__func__));
  785. goto eh_bus_reset_done;
  786. }
  787. if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
  788. if (qla2x00_loop_reset(ha) == QLA_SUCCESS)
  789. ret = SUCCESS;
  790. }
  791. if (ret == FAILED)
  792. goto eh_bus_reset_done;
  793. /* Flush outstanding commands. */
  794. if (!qla2x00_eh_wait_for_pending_commands(ha))
  795. ret = FAILED;
  796. eh_bus_reset_done:
  797. qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
  798. (ret == FAILED) ? "failed" : "succeded");
  799. return ret;
  800. }
  801. /**************************************************************************
  802. * qla2xxx_eh_host_reset
  803. *
  804. * Description:
  805. * The reset function will reset the Adapter.
  806. *
  807. * Input:
  808. * cmd = Linux SCSI command packet of the command that cause the
  809. * adapter reset.
  810. *
  811. * Returns:
  812. * Either SUCCESS or FAILED.
  813. *
  814. * Note:
  815. **************************************************************************/
  816. static int
  817. qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
  818. {
  819. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  820. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  821. int ret;
  822. unsigned int id, lun;
  823. unsigned long serial;
  824. qla2x00_block_error_handler(cmd);
  825. ret = FAILED;
  826. id = cmd->device->id;
  827. lun = cmd->device->lun;
  828. serial = cmd->serial_number;
  829. if (!fcport)
  830. return ret;
  831. qla_printk(KERN_INFO, ha,
  832. "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no, id, lun);
  833. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  834. goto eh_host_reset_lock;
  835. /*
  836. * Fixme-may be dpc thread is active and processing
  837. * loop_resync,so wait a while for it to
  838. * be completed and then issue big hammer.Otherwise
  839. * it may cause I/O failure as big hammer marks the
  840. * devices as lost kicking of the port_down_timer
  841. * while dpc is stuck for the mailbox to complete.
  842. */
  843. qla2x00_wait_for_loop_ready(ha);
  844. set_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  845. if (qla2x00_abort_isp(ha)) {
  846. clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  847. /* failed. schedule dpc to try */
  848. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  849. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  850. goto eh_host_reset_lock;
  851. }
  852. clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  853. /* Waiting for our command in done_queue to be returned to OS.*/
  854. if (qla2x00_eh_wait_for_pending_commands(ha))
  855. ret = SUCCESS;
  856. eh_host_reset_lock:
  857. qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
  858. (ret == FAILED) ? "failed" : "succeded");
  859. return ret;
  860. }
  861. /*
  862. * qla2x00_loop_reset
  863. * Issue loop reset.
  864. *
  865. * Input:
  866. * ha = adapter block pointer.
  867. *
  868. * Returns:
  869. * 0 = success
  870. */
  871. static int
  872. qla2x00_loop_reset(scsi_qla_host_t *ha)
  873. {
  874. int status = QLA_SUCCESS;
  875. struct fc_port *fcport;
  876. if (ha->flags.enable_lip_reset) {
  877. status = qla2x00_lip_reset(ha);
  878. }
  879. if (status == QLA_SUCCESS && ha->flags.enable_target_reset) {
  880. list_for_each_entry(fcport, &ha->fcports, list) {
  881. if (fcport->port_type != FCT_TARGET)
  882. continue;
  883. status = qla2x00_device_reset(ha, fcport);
  884. if (status != QLA_SUCCESS)
  885. break;
  886. }
  887. }
  888. if (status == QLA_SUCCESS &&
  889. ((!ha->flags.enable_target_reset &&
  890. !ha->flags.enable_lip_reset) ||
  891. ha->flags.enable_lip_full_login)) {
  892. status = qla2x00_full_login_lip(ha);
  893. }
  894. /* Issue marker command only when we are going to start the I/O */
  895. ha->marker_needed = 1;
  896. if (status) {
  897. /* Empty */
  898. DEBUG2_3(printk("%s(%ld): **** FAILED ****\n",
  899. __func__,
  900. ha->host_no));
  901. } else {
  902. /* Empty */
  903. DEBUG3(printk("%s(%ld): exiting normally.\n",
  904. __func__,
  905. ha->host_no));
  906. }
  907. return(status);
  908. }
  909. /*
  910. * qla2x00_device_reset
  911. * Issue bus device reset message to the target.
  912. *
  913. * Input:
  914. * ha = adapter block pointer.
  915. * t = SCSI ID.
  916. * TARGET_QUEUE_LOCK must be released.
  917. * ADAPTER_STATE_LOCK must be released.
  918. *
  919. * Context:
  920. * Kernel context.
  921. */
  922. static int
  923. qla2x00_device_reset(scsi_qla_host_t *ha, fc_port_t *reset_fcport)
  924. {
  925. /* Abort Target command will clear Reservation */
  926. return ha->isp_ops.abort_target(reset_fcport);
  927. }
  928. static int
  929. qla2xxx_slave_alloc(struct scsi_device *sdev)
  930. {
  931. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  932. if (!rport || fc_remote_port_chkready(rport))
  933. return -ENXIO;
  934. sdev->hostdata = *(fc_port_t **)rport->dd_data;
  935. return 0;
  936. }
  937. static int
  938. qla2xxx_slave_configure(struct scsi_device *sdev)
  939. {
  940. scsi_qla_host_t *ha = to_qla_host(sdev->host);
  941. struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
  942. if (sdev->tagged_supported)
  943. scsi_activate_tcq(sdev, ha->max_q_depth);
  944. else
  945. scsi_deactivate_tcq(sdev, ha->max_q_depth);
  946. rport->dev_loss_tmo = ha->port_down_retry_count + 5;
  947. return 0;
  948. }
  949. static void
  950. qla2xxx_slave_destroy(struct scsi_device *sdev)
  951. {
  952. sdev->hostdata = NULL;
  953. }
  954. static int
  955. qla2x00_change_queue_depth(struct scsi_device *sdev, int qdepth)
  956. {
  957. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  958. return sdev->queue_depth;
  959. }
  960. static int
  961. qla2x00_change_queue_type(struct scsi_device *sdev, int tag_type)
  962. {
  963. if (sdev->tagged_supported) {
  964. scsi_set_tag_type(sdev, tag_type);
  965. if (tag_type)
  966. scsi_activate_tcq(sdev, sdev->queue_depth);
  967. else
  968. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  969. } else
  970. tag_type = 0;
  971. return tag_type;
  972. }
  973. /**
  974. * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
  975. * @ha: HA context
  976. *
  977. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  978. * supported addressing method.
  979. */
  980. static void
  981. qla2x00_config_dma_addressing(scsi_qla_host_t *ha)
  982. {
  983. /* Assume a 32bit DMA mask. */
  984. ha->flags.enable_64bit_addressing = 0;
  985. if (!dma_set_mask(&ha->pdev->dev, DMA_64BIT_MASK)) {
  986. /* Any upper-dword bits set? */
  987. if (MSD(dma_get_required_mask(&ha->pdev->dev)) &&
  988. !pci_set_consistent_dma_mask(ha->pdev, DMA_64BIT_MASK)) {
  989. /* Ok, a 64bit DMA mask is applicable. */
  990. ha->flags.enable_64bit_addressing = 1;
  991. ha->isp_ops.calc_req_entries = qla2x00_calc_iocbs_64;
  992. ha->isp_ops.build_iocbs = qla2x00_build_scsi_iocbs_64;
  993. return;
  994. }
  995. }
  996. dma_set_mask(&ha->pdev->dev, DMA_32BIT_MASK);
  997. pci_set_consistent_dma_mask(ha->pdev, DMA_32BIT_MASK);
  998. }
  999. static inline void
  1000. qla2x00_set_isp_flags(scsi_qla_host_t *ha)
  1001. {
  1002. ha->device_type = DT_EXTENDED_IDS;
  1003. switch (ha->pdev->device) {
  1004. case PCI_DEVICE_ID_QLOGIC_ISP2100:
  1005. ha->device_type |= DT_ISP2100;
  1006. ha->device_type &= ~DT_EXTENDED_IDS;
  1007. ha->fw_srisc_address = RISC_START_ADDRESS_2100;
  1008. break;
  1009. case PCI_DEVICE_ID_QLOGIC_ISP2200:
  1010. ha->device_type |= DT_ISP2200;
  1011. ha->device_type &= ~DT_EXTENDED_IDS;
  1012. ha->fw_srisc_address = RISC_START_ADDRESS_2100;
  1013. break;
  1014. case PCI_DEVICE_ID_QLOGIC_ISP2300:
  1015. ha->device_type |= DT_ISP2300;
  1016. ha->device_type |= DT_ZIO_SUPPORTED;
  1017. ha->fw_srisc_address = RISC_START_ADDRESS_2300;
  1018. break;
  1019. case PCI_DEVICE_ID_QLOGIC_ISP2312:
  1020. ha->device_type |= DT_ISP2312;
  1021. ha->device_type |= DT_ZIO_SUPPORTED;
  1022. ha->fw_srisc_address = RISC_START_ADDRESS_2300;
  1023. break;
  1024. case PCI_DEVICE_ID_QLOGIC_ISP2322:
  1025. ha->device_type |= DT_ISP2322;
  1026. ha->device_type |= DT_ZIO_SUPPORTED;
  1027. if (ha->pdev->subsystem_vendor == 0x1028 &&
  1028. ha->pdev->subsystem_device == 0x0170)
  1029. ha->device_type |= DT_OEM_001;
  1030. ha->fw_srisc_address = RISC_START_ADDRESS_2300;
  1031. break;
  1032. case PCI_DEVICE_ID_QLOGIC_ISP6312:
  1033. ha->device_type |= DT_ISP6312;
  1034. ha->fw_srisc_address = RISC_START_ADDRESS_2300;
  1035. break;
  1036. case PCI_DEVICE_ID_QLOGIC_ISP6322:
  1037. ha->device_type |= DT_ISP6322;
  1038. ha->fw_srisc_address = RISC_START_ADDRESS_2300;
  1039. break;
  1040. case PCI_DEVICE_ID_QLOGIC_ISP2422:
  1041. ha->device_type |= DT_ISP2422;
  1042. ha->device_type |= DT_ZIO_SUPPORTED;
  1043. ha->fw_srisc_address = RISC_START_ADDRESS_2400;
  1044. break;
  1045. case PCI_DEVICE_ID_QLOGIC_ISP2432:
  1046. ha->device_type |= DT_ISP2432;
  1047. ha->device_type |= DT_ZIO_SUPPORTED;
  1048. ha->fw_srisc_address = RISC_START_ADDRESS_2400;
  1049. break;
  1050. case PCI_DEVICE_ID_QLOGIC_ISP5422:
  1051. ha->device_type |= DT_ISP5422;
  1052. ha->fw_srisc_address = RISC_START_ADDRESS_2400;
  1053. break;
  1054. case PCI_DEVICE_ID_QLOGIC_ISP5432:
  1055. ha->device_type |= DT_ISP5432;
  1056. ha->fw_srisc_address = RISC_START_ADDRESS_2400;
  1057. break;
  1058. }
  1059. }
  1060. static int
  1061. qla2x00_iospace_config(scsi_qla_host_t *ha)
  1062. {
  1063. unsigned long pio, pio_len, pio_flags;
  1064. unsigned long mmio, mmio_len, mmio_flags;
  1065. /* We only need PIO for Flash operations on ISP2312 v2 chips. */
  1066. pio = pci_resource_start(ha->pdev, 0);
  1067. pio_len = pci_resource_len(ha->pdev, 0);
  1068. pio_flags = pci_resource_flags(ha->pdev, 0);
  1069. if (pio_flags & IORESOURCE_IO) {
  1070. if (pio_len < MIN_IOBASE_LEN) {
  1071. qla_printk(KERN_WARNING, ha,
  1072. "Invalid PCI I/O region size (%s)...\n",
  1073. pci_name(ha->pdev));
  1074. pio = 0;
  1075. }
  1076. } else {
  1077. qla_printk(KERN_WARNING, ha,
  1078. "region #0 not a PIO resource (%s)...\n",
  1079. pci_name(ha->pdev));
  1080. pio = 0;
  1081. }
  1082. /* Use MMIO operations for all accesses. */
  1083. mmio = pci_resource_start(ha->pdev, 1);
  1084. mmio_len = pci_resource_len(ha->pdev, 1);
  1085. mmio_flags = pci_resource_flags(ha->pdev, 1);
  1086. if (!(mmio_flags & IORESOURCE_MEM)) {
  1087. qla_printk(KERN_ERR, ha,
  1088. "region #0 not an MMIO resource (%s), aborting\n",
  1089. pci_name(ha->pdev));
  1090. goto iospace_error_exit;
  1091. }
  1092. if (mmio_len < MIN_IOBASE_LEN) {
  1093. qla_printk(KERN_ERR, ha,
  1094. "Invalid PCI mem region size (%s), aborting\n",
  1095. pci_name(ha->pdev));
  1096. goto iospace_error_exit;
  1097. }
  1098. if (pci_request_regions(ha->pdev, QLA2XXX_DRIVER_NAME)) {
  1099. qla_printk(KERN_WARNING, ha,
  1100. "Failed to reserve PIO/MMIO regions (%s)\n",
  1101. pci_name(ha->pdev));
  1102. goto iospace_error_exit;
  1103. }
  1104. ha->pio_address = pio;
  1105. ha->pio_length = pio_len;
  1106. ha->iobase = ioremap(mmio, MIN_IOBASE_LEN);
  1107. if (!ha->iobase) {
  1108. qla_printk(KERN_ERR, ha,
  1109. "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
  1110. goto iospace_error_exit;
  1111. }
  1112. return (0);
  1113. iospace_error_exit:
  1114. return (-ENOMEM);
  1115. }
  1116. static void
  1117. qla2x00_enable_intrs(scsi_qla_host_t *ha)
  1118. {
  1119. unsigned long flags = 0;
  1120. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1121. spin_lock_irqsave(&ha->hardware_lock, flags);
  1122. ha->interrupts_on = 1;
  1123. /* enable risc and host interrupts */
  1124. WRT_REG_WORD(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
  1125. RD_REG_WORD(&reg->ictrl);
  1126. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1127. }
  1128. static void
  1129. qla2x00_disable_intrs(scsi_qla_host_t *ha)
  1130. {
  1131. unsigned long flags = 0;
  1132. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1133. spin_lock_irqsave(&ha->hardware_lock, flags);
  1134. ha->interrupts_on = 0;
  1135. /* disable risc and host interrupts */
  1136. WRT_REG_WORD(&reg->ictrl, 0);
  1137. RD_REG_WORD(&reg->ictrl);
  1138. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1139. }
  1140. static void
  1141. qla24xx_enable_intrs(scsi_qla_host_t *ha)
  1142. {
  1143. unsigned long flags = 0;
  1144. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1145. spin_lock_irqsave(&ha->hardware_lock, flags);
  1146. ha->interrupts_on = 1;
  1147. WRT_REG_DWORD(&reg->ictrl, ICRX_EN_RISC_INT);
  1148. RD_REG_DWORD(&reg->ictrl);
  1149. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1150. }
  1151. static void
  1152. qla24xx_disable_intrs(scsi_qla_host_t *ha)
  1153. {
  1154. unsigned long flags = 0;
  1155. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1156. spin_lock_irqsave(&ha->hardware_lock, flags);
  1157. ha->interrupts_on = 0;
  1158. WRT_REG_DWORD(&reg->ictrl, 0);
  1159. RD_REG_DWORD(&reg->ictrl);
  1160. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1161. }
  1162. /*
  1163. * PCI driver interface
  1164. */
  1165. static int __devinit
  1166. qla2x00_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  1167. {
  1168. int ret = -ENODEV;
  1169. device_reg_t __iomem *reg;
  1170. struct Scsi_Host *host;
  1171. scsi_qla_host_t *ha;
  1172. unsigned long flags = 0;
  1173. char pci_info[20];
  1174. char fw_str[30];
  1175. struct scsi_host_template *sht;
  1176. if (pci_enable_device(pdev))
  1177. goto probe_out;
  1178. sht = &qla2x00_driver_template;
  1179. if (pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2422 ||
  1180. pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2432)
  1181. sht = &qla24xx_driver_template;
  1182. host = scsi_host_alloc(sht, sizeof(scsi_qla_host_t));
  1183. if (host == NULL) {
  1184. printk(KERN_WARNING
  1185. "qla2xxx: Couldn't allocate host from scsi layer!\n");
  1186. goto probe_disable_device;
  1187. }
  1188. /* Clear our data area */
  1189. ha = (scsi_qla_host_t *)host->hostdata;
  1190. memset(ha, 0, sizeof(scsi_qla_host_t));
  1191. ha->pdev = pdev;
  1192. ha->host = host;
  1193. ha->host_no = host->host_no;
  1194. sprintf(ha->host_str, "%s_%ld", QLA2XXX_DRIVER_NAME, ha->host_no);
  1195. /* Set ISP-type information. */
  1196. qla2x00_set_isp_flags(ha);
  1197. /* Configure PCI I/O space */
  1198. ret = qla2x00_iospace_config(ha);
  1199. if (ret)
  1200. goto probe_failed;
  1201. qla_printk(KERN_INFO, ha,
  1202. "Found an ISP%04X, irq %d, iobase 0x%p\n", pdev->device, pdev->irq,
  1203. ha->iobase);
  1204. spin_lock_init(&ha->hardware_lock);
  1205. ha->prev_topology = 0;
  1206. ha->init_cb_size = sizeof(init_cb_t);
  1207. ha->mgmt_svr_loop_id = MANAGEMENT_SERVER;
  1208. ha->link_data_rate = PORT_SPEED_UNKNOWN;
  1209. ha->optrom_size = OPTROM_SIZE_2300;
  1210. ha->max_q_depth = MAX_Q_DEPTH;
  1211. if (ql2xmaxqdepth != 0 && ql2xmaxqdepth <= 0xffffU)
  1212. ha->max_q_depth = ql2xmaxqdepth;
  1213. /* Assign ISP specific operations. */
  1214. ha->isp_ops.pci_config = qla2100_pci_config;
  1215. ha->isp_ops.reset_chip = qla2x00_reset_chip;
  1216. ha->isp_ops.chip_diag = qla2x00_chip_diag;
  1217. ha->isp_ops.config_rings = qla2x00_config_rings;
  1218. ha->isp_ops.reset_adapter = qla2x00_reset_adapter;
  1219. ha->isp_ops.nvram_config = qla2x00_nvram_config;
  1220. ha->isp_ops.update_fw_options = qla2x00_update_fw_options;
  1221. ha->isp_ops.load_risc = qla2x00_load_risc;
  1222. ha->isp_ops.pci_info_str = qla2x00_pci_info_str;
  1223. ha->isp_ops.fw_version_str = qla2x00_fw_version_str;
  1224. ha->isp_ops.intr_handler = qla2100_intr_handler;
  1225. ha->isp_ops.enable_intrs = qla2x00_enable_intrs;
  1226. ha->isp_ops.disable_intrs = qla2x00_disable_intrs;
  1227. ha->isp_ops.abort_command = qla2x00_abort_command;
  1228. ha->isp_ops.abort_target = qla2x00_abort_target;
  1229. ha->isp_ops.fabric_login = qla2x00_login_fabric;
  1230. ha->isp_ops.fabric_logout = qla2x00_fabric_logout;
  1231. ha->isp_ops.calc_req_entries = qla2x00_calc_iocbs_32;
  1232. ha->isp_ops.build_iocbs = qla2x00_build_scsi_iocbs_32;
  1233. ha->isp_ops.prep_ms_iocb = qla2x00_prep_ms_iocb;
  1234. ha->isp_ops.prep_ms_fdmi_iocb = qla2x00_prep_ms_fdmi_iocb;
  1235. ha->isp_ops.read_nvram = qla2x00_read_nvram_data;
  1236. ha->isp_ops.write_nvram = qla2x00_write_nvram_data;
  1237. ha->isp_ops.fw_dump = qla2100_fw_dump;
  1238. ha->isp_ops.read_optrom = qla2x00_read_optrom_data;
  1239. ha->isp_ops.write_optrom = qla2x00_write_optrom_data;
  1240. if (IS_QLA2100(ha)) {
  1241. host->max_id = MAX_TARGETS_2100;
  1242. ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
  1243. ha->request_q_length = REQUEST_ENTRY_CNT_2100;
  1244. ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
  1245. ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
  1246. host->sg_tablesize = 32;
  1247. ha->gid_list_info_size = 4;
  1248. } else if (IS_QLA2200(ha)) {
  1249. host->max_id = MAX_TARGETS_2200;
  1250. ha->mbx_count = MAILBOX_REGISTER_COUNT;
  1251. ha->request_q_length = REQUEST_ENTRY_CNT_2200;
  1252. ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
  1253. ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
  1254. ha->gid_list_info_size = 4;
  1255. } else if (IS_QLA23XX(ha)) {
  1256. host->max_id = MAX_TARGETS_2200;
  1257. ha->mbx_count = MAILBOX_REGISTER_COUNT;
  1258. ha->request_q_length = REQUEST_ENTRY_CNT_2200;
  1259. ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
  1260. ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
  1261. ha->isp_ops.pci_config = qla2300_pci_config;
  1262. ha->isp_ops.intr_handler = qla2300_intr_handler;
  1263. ha->isp_ops.fw_dump = qla2300_fw_dump;
  1264. ha->isp_ops.beacon_on = qla2x00_beacon_on;
  1265. ha->isp_ops.beacon_off = qla2x00_beacon_off;
  1266. ha->isp_ops.beacon_blink = qla2x00_beacon_blink;
  1267. ha->gid_list_info_size = 6;
  1268. if (IS_QLA2322(ha) || IS_QLA6322(ha))
  1269. ha->optrom_size = OPTROM_SIZE_2322;
  1270. } else if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
  1271. host->max_id = MAX_TARGETS_2200;
  1272. ha->mbx_count = MAILBOX_REGISTER_COUNT;
  1273. ha->request_q_length = REQUEST_ENTRY_CNT_24XX;
  1274. ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
  1275. ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
  1276. ha->init_cb_size = sizeof(struct init_cb_24xx);
  1277. ha->mgmt_svr_loop_id = 10;
  1278. ha->isp_ops.pci_config = qla24xx_pci_config;
  1279. ha->isp_ops.reset_chip = qla24xx_reset_chip;
  1280. ha->isp_ops.chip_diag = qla24xx_chip_diag;
  1281. ha->isp_ops.config_rings = qla24xx_config_rings;
  1282. ha->isp_ops.reset_adapter = qla24xx_reset_adapter;
  1283. ha->isp_ops.nvram_config = qla24xx_nvram_config;
  1284. ha->isp_ops.update_fw_options = qla24xx_update_fw_options;
  1285. ha->isp_ops.load_risc = qla24xx_load_risc;
  1286. ha->isp_ops.pci_info_str = qla24xx_pci_info_str;
  1287. ha->isp_ops.fw_version_str = qla24xx_fw_version_str;
  1288. ha->isp_ops.intr_handler = qla24xx_intr_handler;
  1289. ha->isp_ops.enable_intrs = qla24xx_enable_intrs;
  1290. ha->isp_ops.disable_intrs = qla24xx_disable_intrs;
  1291. ha->isp_ops.abort_command = qla24xx_abort_command;
  1292. ha->isp_ops.abort_target = qla24xx_abort_target;
  1293. ha->isp_ops.fabric_login = qla24xx_login_fabric;
  1294. ha->isp_ops.fabric_logout = qla24xx_fabric_logout;
  1295. ha->isp_ops.prep_ms_iocb = qla24xx_prep_ms_iocb;
  1296. ha->isp_ops.prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb;
  1297. ha->isp_ops.read_nvram = qla24xx_read_nvram_data;
  1298. ha->isp_ops.write_nvram = qla24xx_write_nvram_data;
  1299. ha->isp_ops.fw_dump = qla24xx_fw_dump;
  1300. ha->isp_ops.read_optrom = qla24xx_read_optrom_data;
  1301. ha->isp_ops.write_optrom = qla24xx_write_optrom_data;
  1302. ha->isp_ops.beacon_on = qla24xx_beacon_on;
  1303. ha->isp_ops.beacon_off = qla24xx_beacon_off;
  1304. ha->isp_ops.beacon_blink = qla24xx_beacon_blink;
  1305. ha->gid_list_info_size = 8;
  1306. ha->optrom_size = OPTROM_SIZE_24XX;
  1307. }
  1308. host->can_queue = ha->request_q_length + 128;
  1309. /* load the F/W, read paramaters, and init the H/W */
  1310. ha->instance = num_hosts;
  1311. init_MUTEX(&ha->mbx_cmd_sem);
  1312. init_MUTEX_LOCKED(&ha->mbx_intr_sem);
  1313. INIT_LIST_HEAD(&ha->list);
  1314. INIT_LIST_HEAD(&ha->fcports);
  1315. /*
  1316. * These locks are used to prevent more than one CPU
  1317. * from modifying the queue at the same time. The
  1318. * higher level "host_lock" will reduce most
  1319. * contention for these locks.
  1320. */
  1321. spin_lock_init(&ha->mbx_reg_lock);
  1322. qla2x00_config_dma_addressing(ha);
  1323. if (qla2x00_mem_alloc(ha)) {
  1324. qla_printk(KERN_WARNING, ha,
  1325. "[ERROR] Failed to allocate memory for adapter\n");
  1326. ret = -ENOMEM;
  1327. goto probe_failed;
  1328. }
  1329. if (qla2x00_initialize_adapter(ha) &&
  1330. !(ha->device_flags & DFLG_NO_CABLE)) {
  1331. qla_printk(KERN_WARNING, ha,
  1332. "Failed to initialize adapter\n");
  1333. DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
  1334. "Adapter flags %x.\n",
  1335. ha->host_no, ha->device_flags));
  1336. ret = -ENODEV;
  1337. goto probe_failed;
  1338. }
  1339. /*
  1340. * Startup the kernel thread for this host adapter
  1341. */
  1342. ha->dpc_thread = kthread_create(qla2x00_do_dpc, ha,
  1343. "%s_dpc", ha->host_str);
  1344. if (IS_ERR(ha->dpc_thread)) {
  1345. qla_printk(KERN_WARNING, ha,
  1346. "Unable to start DPC thread!\n");
  1347. ret = PTR_ERR(ha->dpc_thread);
  1348. goto probe_failed;
  1349. }
  1350. host->this_id = 255;
  1351. host->cmd_per_lun = 3;
  1352. host->unique_id = ha->instance;
  1353. host->max_cmd_len = MAX_CMDSZ;
  1354. host->max_channel = MAX_BUSES - 1;
  1355. host->max_lun = MAX_LUNS;
  1356. host->transportt = qla2xxx_transport_template;
  1357. ret = request_irq(pdev->irq, ha->isp_ops.intr_handler,
  1358. IRQF_DISABLED|IRQF_SHARED, QLA2XXX_DRIVER_NAME, ha);
  1359. if (ret) {
  1360. qla_printk(KERN_WARNING, ha,
  1361. "Failed to reserve interrupt %d already in use.\n",
  1362. pdev->irq);
  1363. goto probe_failed;
  1364. }
  1365. host->irq = pdev->irq;
  1366. /* Initialized the timer */
  1367. qla2x00_start_timer(ha, qla2x00_timer, WATCH_INTERVAL);
  1368. DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
  1369. ha->host_no, ha));
  1370. ha->isp_ops.disable_intrs(ha);
  1371. spin_lock_irqsave(&ha->hardware_lock, flags);
  1372. reg = ha->iobase;
  1373. if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
  1374. WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_HOST_INT);
  1375. WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_RISC_INT);
  1376. } else {
  1377. WRT_REG_WORD(&reg->isp.semaphore, 0);
  1378. WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_RISC_INT);
  1379. WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_HOST_INT);
  1380. /* Enable proper parity */
  1381. if (!IS_QLA2100(ha) && !IS_QLA2200(ha)) {
  1382. if (IS_QLA2300(ha))
  1383. /* SRAM parity */
  1384. WRT_REG_WORD(&reg->isp.hccr,
  1385. (HCCR_ENABLE_PARITY + 0x1));
  1386. else
  1387. /* SRAM, Instruction RAM and GP RAM parity */
  1388. WRT_REG_WORD(&reg->isp.hccr,
  1389. (HCCR_ENABLE_PARITY + 0x7));
  1390. }
  1391. }
  1392. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1393. ha->isp_ops.enable_intrs(ha);
  1394. pci_set_drvdata(pdev, ha);
  1395. /* Start link scan. */
  1396. set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  1397. set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
  1398. set_bit(RSCN_UPDATE, &ha->dpc_flags);
  1399. ha->flags.init_done = 1;
  1400. ha->flags.online = 1;
  1401. num_hosts++;
  1402. ret = scsi_add_host(host, &pdev->dev);
  1403. if (ret)
  1404. goto probe_failed;
  1405. qla2x00_alloc_sysfs_attr(ha);
  1406. qla2x00_init_host_attr(ha);
  1407. qla_printk(KERN_INFO, ha, "\n"
  1408. " QLogic Fibre Channel HBA Driver: %s\n"
  1409. " QLogic %s - %s\n"
  1410. " ISP%04X: %s @ %s hdma%c, host#=%ld, fw=%s\n",
  1411. qla2x00_version_str, ha->model_number,
  1412. ha->model_desc ? ha->model_desc: "", pdev->device,
  1413. ha->isp_ops.pci_info_str(ha, pci_info), pci_name(pdev),
  1414. ha->flags.enable_64bit_addressing ? '+': '-', ha->host_no,
  1415. ha->isp_ops.fw_version_str(ha, fw_str));
  1416. return 0;
  1417. probe_failed:
  1418. qla2x00_free_device(ha);
  1419. scsi_host_put(host);
  1420. probe_disable_device:
  1421. pci_disable_device(pdev);
  1422. probe_out:
  1423. return ret;
  1424. }
  1425. static void __devexit
  1426. qla2x00_remove_one(struct pci_dev *pdev)
  1427. {
  1428. scsi_qla_host_t *ha;
  1429. ha = pci_get_drvdata(pdev);
  1430. qla2x00_free_sysfs_attr(ha);
  1431. fc_remove_host(ha->host);
  1432. scsi_remove_host(ha->host);
  1433. qla2x00_free_device(ha);
  1434. scsi_host_put(ha->host);
  1435. pci_set_drvdata(pdev, NULL);
  1436. }
  1437. static void
  1438. qla2x00_free_device(scsi_qla_host_t *ha)
  1439. {
  1440. /* Disable timer */
  1441. if (ha->timer_active)
  1442. qla2x00_stop_timer(ha);
  1443. /* Kill the kernel thread for this host */
  1444. if (ha->dpc_thread) {
  1445. struct task_struct *t = ha->dpc_thread;
  1446. /*
  1447. * qla2xxx_wake_dpc checks for ->dpc_thread
  1448. * so we need to zero it out.
  1449. */
  1450. ha->dpc_thread = NULL;
  1451. kthread_stop(t);
  1452. }
  1453. if (ha->eft)
  1454. qla2x00_trace_control(ha, TC_DISABLE, 0, 0);
  1455. ha->flags.online = 0;
  1456. /* Stop currently executing firmware. */
  1457. qla2x00_try_to_stop_firmware(ha);
  1458. /* turn-off interrupts on the card */
  1459. if (ha->interrupts_on)
  1460. ha->isp_ops.disable_intrs(ha);
  1461. qla2x00_mem_free(ha);
  1462. /* Detach interrupts */
  1463. if (ha->host->irq)
  1464. free_irq(ha->host->irq, ha);
  1465. /* release io space registers */
  1466. if (ha->iobase)
  1467. iounmap(ha->iobase);
  1468. pci_release_regions(ha->pdev);
  1469. pci_disable_device(ha->pdev);
  1470. }
  1471. static inline void
  1472. qla2x00_schedule_rport_del(struct scsi_qla_host *ha, fc_port_t *fcport,
  1473. int defer)
  1474. {
  1475. unsigned long flags;
  1476. struct fc_rport *rport;
  1477. if (!fcport->rport)
  1478. return;
  1479. rport = fcport->rport;
  1480. if (defer) {
  1481. spin_lock_irqsave(&fcport->rport_lock, flags);
  1482. fcport->drport = rport;
  1483. fcport->rport = NULL;
  1484. *(fc_port_t **)rport->dd_data = NULL;
  1485. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1486. set_bit(FCPORT_UPDATE_NEEDED, &ha->dpc_flags);
  1487. } else {
  1488. spin_lock_irqsave(&fcport->rport_lock, flags);
  1489. fcport->rport = NULL;
  1490. *(fc_port_t **)rport->dd_data = NULL;
  1491. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1492. fc_remote_port_delete(rport);
  1493. }
  1494. }
  1495. /*
  1496. * qla2x00_mark_device_lost Updates fcport state when device goes offline.
  1497. *
  1498. * Input: ha = adapter block pointer. fcport = port structure pointer.
  1499. *
  1500. * Return: None.
  1501. *
  1502. * Context:
  1503. */
  1504. void qla2x00_mark_device_lost(scsi_qla_host_t *ha, fc_port_t *fcport,
  1505. int do_login, int defer)
  1506. {
  1507. if (atomic_read(&fcport->state) == FCS_ONLINE)
  1508. qla2x00_schedule_rport_del(ha, fcport, defer);
  1509. /*
  1510. * We may need to retry the login, so don't change the state of the
  1511. * port but do the retries.
  1512. */
  1513. if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
  1514. atomic_set(&fcport->state, FCS_DEVICE_LOST);
  1515. if (!do_login)
  1516. return;
  1517. if (fcport->login_retry == 0) {
  1518. fcport->login_retry = ha->login_retry_count;
  1519. set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
  1520. DEBUG(printk("scsi(%ld): Port login retry: "
  1521. "%02x%02x%02x%02x%02x%02x%02x%02x, "
  1522. "id = 0x%04x retry cnt=%d\n",
  1523. ha->host_no,
  1524. fcport->port_name[0],
  1525. fcport->port_name[1],
  1526. fcport->port_name[2],
  1527. fcport->port_name[3],
  1528. fcport->port_name[4],
  1529. fcport->port_name[5],
  1530. fcport->port_name[6],
  1531. fcport->port_name[7],
  1532. fcport->loop_id,
  1533. fcport->login_retry));
  1534. }
  1535. }
  1536. /*
  1537. * qla2x00_mark_all_devices_lost
  1538. * Updates fcport state when device goes offline.
  1539. *
  1540. * Input:
  1541. * ha = adapter block pointer.
  1542. * fcport = port structure pointer.
  1543. *
  1544. * Return:
  1545. * None.
  1546. *
  1547. * Context:
  1548. */
  1549. void
  1550. qla2x00_mark_all_devices_lost(scsi_qla_host_t *ha, int defer)
  1551. {
  1552. fc_port_t *fcport;
  1553. list_for_each_entry(fcport, &ha->fcports, list) {
  1554. if (fcport->port_type != FCT_TARGET)
  1555. continue;
  1556. /*
  1557. * No point in marking the device as lost, if the device is
  1558. * already DEAD.
  1559. */
  1560. if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
  1561. continue;
  1562. if (atomic_read(&fcport->state) == FCS_ONLINE)
  1563. qla2x00_schedule_rport_del(ha, fcport, defer);
  1564. atomic_set(&fcport->state, FCS_DEVICE_LOST);
  1565. }
  1566. if (defer)
  1567. qla2xxx_wake_dpc(ha);
  1568. }
  1569. /*
  1570. * qla2x00_mem_alloc
  1571. * Allocates adapter memory.
  1572. *
  1573. * Returns:
  1574. * 0 = success.
  1575. * 1 = failure.
  1576. */
  1577. static uint8_t
  1578. qla2x00_mem_alloc(scsi_qla_host_t *ha)
  1579. {
  1580. char name[16];
  1581. uint8_t status = 1;
  1582. int retry= 10;
  1583. do {
  1584. /*
  1585. * This will loop only once if everything goes well, else some
  1586. * number of retries will be performed to get around a kernel
  1587. * bug where available mem is not allocated until after a
  1588. * little delay and a retry.
  1589. */
  1590. ha->request_ring = dma_alloc_coherent(&ha->pdev->dev,
  1591. (ha->request_q_length + 1) * sizeof(request_t),
  1592. &ha->request_dma, GFP_KERNEL);
  1593. if (ha->request_ring == NULL) {
  1594. qla_printk(KERN_WARNING, ha,
  1595. "Memory Allocation failed - request_ring\n");
  1596. qla2x00_mem_free(ha);
  1597. msleep(100);
  1598. continue;
  1599. }
  1600. ha->response_ring = dma_alloc_coherent(&ha->pdev->dev,
  1601. (ha->response_q_length + 1) * sizeof(response_t),
  1602. &ha->response_dma, GFP_KERNEL);
  1603. if (ha->response_ring == NULL) {
  1604. qla_printk(KERN_WARNING, ha,
  1605. "Memory Allocation failed - response_ring\n");
  1606. qla2x00_mem_free(ha);
  1607. msleep(100);
  1608. continue;
  1609. }
  1610. ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
  1611. &ha->gid_list_dma, GFP_KERNEL);
  1612. if (ha->gid_list == NULL) {
  1613. qla_printk(KERN_WARNING, ha,
  1614. "Memory Allocation failed - gid_list\n");
  1615. qla2x00_mem_free(ha);
  1616. msleep(100);
  1617. continue;
  1618. }
  1619. snprintf(name, sizeof(name), "%s_%ld", QLA2XXX_DRIVER_NAME,
  1620. ha->host_no);
  1621. ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
  1622. DMA_POOL_SIZE, 8, 0);
  1623. if (ha->s_dma_pool == NULL) {
  1624. qla_printk(KERN_WARNING, ha,
  1625. "Memory Allocation failed - s_dma_pool\n");
  1626. qla2x00_mem_free(ha);
  1627. msleep(100);
  1628. continue;
  1629. }
  1630. /* get consistent memory allocated for init control block */
  1631. ha->init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
  1632. &ha->init_cb_dma);
  1633. if (ha->init_cb == NULL) {
  1634. qla_printk(KERN_WARNING, ha,
  1635. "Memory Allocation failed - init_cb\n");
  1636. qla2x00_mem_free(ha);
  1637. msleep(100);
  1638. continue;
  1639. }
  1640. memset(ha->init_cb, 0, ha->init_cb_size);
  1641. if (qla2x00_allocate_sp_pool(ha)) {
  1642. qla_printk(KERN_WARNING, ha,
  1643. "Memory Allocation failed - "
  1644. "qla2x00_allocate_sp_pool()\n");
  1645. qla2x00_mem_free(ha);
  1646. msleep(100);
  1647. continue;
  1648. }
  1649. /* Allocate memory for SNS commands */
  1650. if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
  1651. /* Get consistent memory allocated for SNS commands */
  1652. ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
  1653. sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma,
  1654. GFP_KERNEL);
  1655. if (ha->sns_cmd == NULL) {
  1656. /* error */
  1657. qla_printk(KERN_WARNING, ha,
  1658. "Memory Allocation failed - sns_cmd\n");
  1659. qla2x00_mem_free(ha);
  1660. msleep(100);
  1661. continue;
  1662. }
  1663. memset(ha->sns_cmd, 0, sizeof(struct sns_cmd_pkt));
  1664. } else {
  1665. /* Get consistent memory allocated for MS IOCB */
  1666. ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
  1667. &ha->ms_iocb_dma);
  1668. if (ha->ms_iocb == NULL) {
  1669. /* error */
  1670. qla_printk(KERN_WARNING, ha,
  1671. "Memory Allocation failed - ms_iocb\n");
  1672. qla2x00_mem_free(ha);
  1673. msleep(100);
  1674. continue;
  1675. }
  1676. memset(ha->ms_iocb, 0, sizeof(ms_iocb_entry_t));
  1677. /*
  1678. * Get consistent memory allocated for CT SNS
  1679. * commands
  1680. */
  1681. ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
  1682. sizeof(struct ct_sns_pkt), &ha->ct_sns_dma,
  1683. GFP_KERNEL);
  1684. if (ha->ct_sns == NULL) {
  1685. /* error */
  1686. qla_printk(KERN_WARNING, ha,
  1687. "Memory Allocation failed - ct_sns\n");
  1688. qla2x00_mem_free(ha);
  1689. msleep(100);
  1690. continue;
  1691. }
  1692. memset(ha->ct_sns, 0, sizeof(struct ct_sns_pkt));
  1693. if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
  1694. /*
  1695. * Get consistent memory allocated for SFP
  1696. * block.
  1697. */
  1698. ha->sfp_data = dma_pool_alloc(ha->s_dma_pool,
  1699. GFP_KERNEL, &ha->sfp_data_dma);
  1700. if (ha->sfp_data == NULL) {
  1701. qla_printk(KERN_WARNING, ha,
  1702. "Memory Allocation failed - "
  1703. "sfp_data\n");
  1704. qla2x00_mem_free(ha);
  1705. msleep(100);
  1706. continue;
  1707. }
  1708. memset(ha->sfp_data, 0, SFP_BLOCK_SIZE);
  1709. }
  1710. }
  1711. /* Done all allocations without any error. */
  1712. status = 0;
  1713. } while (retry-- && status != 0);
  1714. if (status) {
  1715. printk(KERN_WARNING
  1716. "%s(): **** FAILED ****\n", __func__);
  1717. }
  1718. return(status);
  1719. }
  1720. /*
  1721. * qla2x00_mem_free
  1722. * Frees all adapter allocated memory.
  1723. *
  1724. * Input:
  1725. * ha = adapter block pointer.
  1726. */
  1727. static void
  1728. qla2x00_mem_free(scsi_qla_host_t *ha)
  1729. {
  1730. struct list_head *fcpl, *fcptemp;
  1731. fc_port_t *fcport;
  1732. if (ha == NULL) {
  1733. /* error */
  1734. DEBUG2(printk("%s(): ERROR invalid ha pointer.\n", __func__));
  1735. return;
  1736. }
  1737. /* free sp pool */
  1738. qla2x00_free_sp_pool(ha);
  1739. if (ha->fw_dump) {
  1740. if (ha->eft)
  1741. dma_free_coherent(&ha->pdev->dev,
  1742. ntohl(ha->fw_dump->eft_size), ha->eft, ha->eft_dma);
  1743. vfree(ha->fw_dump);
  1744. }
  1745. if (ha->sns_cmd)
  1746. dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
  1747. ha->sns_cmd, ha->sns_cmd_dma);
  1748. if (ha->ct_sns)
  1749. dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
  1750. ha->ct_sns, ha->ct_sns_dma);
  1751. if (ha->sfp_data)
  1752. dma_pool_free(ha->s_dma_pool, ha->sfp_data, ha->sfp_data_dma);
  1753. if (ha->ms_iocb)
  1754. dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
  1755. if (ha->init_cb)
  1756. dma_pool_free(ha->s_dma_pool, ha->init_cb, ha->init_cb_dma);
  1757. if (ha->s_dma_pool)
  1758. dma_pool_destroy(ha->s_dma_pool);
  1759. if (ha->gid_list)
  1760. dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
  1761. ha->gid_list_dma);
  1762. if (ha->response_ring)
  1763. dma_free_coherent(&ha->pdev->dev,
  1764. (ha->response_q_length + 1) * sizeof(response_t),
  1765. ha->response_ring, ha->response_dma);
  1766. if (ha->request_ring)
  1767. dma_free_coherent(&ha->pdev->dev,
  1768. (ha->request_q_length + 1) * sizeof(request_t),
  1769. ha->request_ring, ha->request_dma);
  1770. ha->eft = NULL;
  1771. ha->eft_dma = 0;
  1772. ha->sns_cmd = NULL;
  1773. ha->sns_cmd_dma = 0;
  1774. ha->ct_sns = NULL;
  1775. ha->ct_sns_dma = 0;
  1776. ha->ms_iocb = NULL;
  1777. ha->ms_iocb_dma = 0;
  1778. ha->init_cb = NULL;
  1779. ha->init_cb_dma = 0;
  1780. ha->s_dma_pool = NULL;
  1781. ha->gid_list = NULL;
  1782. ha->gid_list_dma = 0;
  1783. ha->response_ring = NULL;
  1784. ha->response_dma = 0;
  1785. ha->request_ring = NULL;
  1786. ha->request_dma = 0;
  1787. list_for_each_safe(fcpl, fcptemp, &ha->fcports) {
  1788. fcport = list_entry(fcpl, fc_port_t, list);
  1789. /* fc ports */
  1790. list_del_init(&fcport->list);
  1791. kfree(fcport);
  1792. }
  1793. INIT_LIST_HEAD(&ha->fcports);
  1794. ha->fw_dump = NULL;
  1795. ha->fw_dumped = 0;
  1796. ha->fw_dump_reading = 0;
  1797. vfree(ha->optrom_buffer);
  1798. }
  1799. /*
  1800. * qla2x00_allocate_sp_pool
  1801. * This routine is called during initialization to allocate
  1802. * memory for local srb_t.
  1803. *
  1804. * Input:
  1805. * ha = adapter block pointer.
  1806. *
  1807. * Context:
  1808. * Kernel context.
  1809. */
  1810. static int
  1811. qla2x00_allocate_sp_pool(scsi_qla_host_t *ha)
  1812. {
  1813. int rval;
  1814. rval = QLA_SUCCESS;
  1815. ha->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
  1816. if (ha->srb_mempool == NULL) {
  1817. qla_printk(KERN_INFO, ha, "Unable to allocate SRB mempool.\n");
  1818. rval = QLA_FUNCTION_FAILED;
  1819. }
  1820. return (rval);
  1821. }
  1822. /*
  1823. * This routine frees all adapter allocated memory.
  1824. *
  1825. */
  1826. static void
  1827. qla2x00_free_sp_pool( scsi_qla_host_t *ha)
  1828. {
  1829. if (ha->srb_mempool) {
  1830. mempool_destroy(ha->srb_mempool);
  1831. ha->srb_mempool = NULL;
  1832. }
  1833. }
  1834. /**************************************************************************
  1835. * qla2x00_do_dpc
  1836. * This kernel thread is a task that is schedule by the interrupt handler
  1837. * to perform the background processing for interrupts.
  1838. *
  1839. * Notes:
  1840. * This task always run in the context of a kernel thread. It
  1841. * is kick-off by the driver's detect code and starts up
  1842. * up one per adapter. It immediately goes to sleep and waits for
  1843. * some fibre event. When either the interrupt handler or
  1844. * the timer routine detects a event it will one of the task
  1845. * bits then wake us up.
  1846. **************************************************************************/
  1847. static int
  1848. qla2x00_do_dpc(void *data)
  1849. {
  1850. scsi_qla_host_t *ha;
  1851. fc_port_t *fcport;
  1852. uint8_t status;
  1853. uint16_t next_loopid;
  1854. ha = (scsi_qla_host_t *)data;
  1855. set_user_nice(current, -20);
  1856. while (!kthread_should_stop()) {
  1857. DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
  1858. set_current_state(TASK_INTERRUPTIBLE);
  1859. schedule();
  1860. __set_current_state(TASK_RUNNING);
  1861. DEBUG3(printk("qla2x00: DPC handler waking up\n"));
  1862. /* Initialization not yet finished. Don't do anything yet. */
  1863. if (!ha->flags.init_done)
  1864. continue;
  1865. DEBUG3(printk("scsi(%ld): DPC handler\n", ha->host_no));
  1866. ha->dpc_active = 1;
  1867. if (ha->flags.mbox_busy) {
  1868. ha->dpc_active = 0;
  1869. continue;
  1870. }
  1871. if (test_and_clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
  1872. DEBUG(printk("scsi(%ld): dpc: sched "
  1873. "qla2x00_abort_isp ha = %p\n",
  1874. ha->host_no, ha));
  1875. if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
  1876. &ha->dpc_flags))) {
  1877. if (qla2x00_abort_isp(ha)) {
  1878. /* failed. retry later */
  1879. set_bit(ISP_ABORT_NEEDED,
  1880. &ha->dpc_flags);
  1881. }
  1882. clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  1883. }
  1884. DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
  1885. ha->host_no));
  1886. }
  1887. if (test_and_clear_bit(FCPORT_UPDATE_NEEDED, &ha->dpc_flags))
  1888. qla2x00_update_fcports(ha);
  1889. if (test_and_clear_bit(LOOP_RESET_NEEDED, &ha->dpc_flags)) {
  1890. DEBUG(printk("scsi(%ld): dpc: sched loop_reset()\n",
  1891. ha->host_no));
  1892. qla2x00_loop_reset(ha);
  1893. }
  1894. if (test_and_clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) &&
  1895. (!(test_and_set_bit(RESET_ACTIVE, &ha->dpc_flags)))) {
  1896. DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
  1897. ha->host_no));
  1898. qla2x00_rst_aen(ha);
  1899. clear_bit(RESET_ACTIVE, &ha->dpc_flags);
  1900. }
  1901. /* Retry each device up to login retry count */
  1902. if ((test_and_clear_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
  1903. !test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) &&
  1904. atomic_read(&ha->loop_state) != LOOP_DOWN) {
  1905. DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
  1906. ha->host_no));
  1907. next_loopid = 0;
  1908. list_for_each_entry(fcport, &ha->fcports, list) {
  1909. /*
  1910. * If the port is not ONLINE then try to login
  1911. * to it if we haven't run out of retries.
  1912. */
  1913. if (atomic_read(&fcport->state) != FCS_ONLINE &&
  1914. fcport->login_retry) {
  1915. fcport->login_retry--;
  1916. if (fcport->flags & FCF_FABRIC_DEVICE) {
  1917. if (fcport->flags &
  1918. FCF_TAPE_PRESENT)
  1919. ha->isp_ops.fabric_logout(
  1920. ha, fcport->loop_id,
  1921. fcport->d_id.b.domain,
  1922. fcport->d_id.b.area,
  1923. fcport->d_id.b.al_pa);
  1924. status = qla2x00_fabric_login(
  1925. ha, fcport, &next_loopid);
  1926. } else
  1927. status =
  1928. qla2x00_local_device_login(
  1929. ha, fcport);
  1930. if (status == QLA_SUCCESS) {
  1931. fcport->old_loop_id = fcport->loop_id;
  1932. DEBUG(printk("scsi(%ld): port login OK: logged in ID 0x%x\n",
  1933. ha->host_no, fcport->loop_id));
  1934. qla2x00_update_fcport(ha,
  1935. fcport);
  1936. } else if (status == 1) {
  1937. set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
  1938. /* retry the login again */
  1939. DEBUG(printk("scsi(%ld): Retrying %d login again loop_id 0x%x\n",
  1940. ha->host_no,
  1941. fcport->login_retry, fcport->loop_id));
  1942. } else {
  1943. fcport->login_retry = 0;
  1944. }
  1945. }
  1946. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  1947. break;
  1948. }
  1949. DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
  1950. ha->host_no));
  1951. }
  1952. if ((test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags)) &&
  1953. atomic_read(&ha->loop_state) != LOOP_DOWN) {
  1954. clear_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags);
  1955. DEBUG(printk("scsi(%ld): qla2x00_login_retry()\n",
  1956. ha->host_no));
  1957. set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  1958. DEBUG(printk("scsi(%ld): qla2x00_login_retry - end\n",
  1959. ha->host_no));
  1960. }
  1961. if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1962. DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
  1963. ha->host_no));
  1964. if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
  1965. &ha->dpc_flags))) {
  1966. qla2x00_loop_resync(ha);
  1967. clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
  1968. }
  1969. DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
  1970. ha->host_no));
  1971. }
  1972. if (test_and_clear_bit(FCPORT_RESCAN_NEEDED, &ha->dpc_flags)) {
  1973. DEBUG(printk("scsi(%ld): Rescan flagged fcports...\n",
  1974. ha->host_no));
  1975. qla2x00_rescan_fcports(ha);
  1976. DEBUG(printk("scsi(%ld): Rescan flagged fcports..."
  1977. "end.\n",
  1978. ha->host_no));
  1979. }
  1980. if (!ha->interrupts_on)
  1981. ha->isp_ops.enable_intrs(ha);
  1982. if (test_and_clear_bit(BEACON_BLINK_NEEDED, &ha->dpc_flags))
  1983. ha->isp_ops.beacon_blink(ha);
  1984. ha->dpc_active = 0;
  1985. } /* End of while(1) */
  1986. DEBUG(printk("scsi(%ld): DPC handler exiting\n", ha->host_no));
  1987. /*
  1988. * Make sure that nobody tries to wake us up again.
  1989. */
  1990. ha->dpc_active = 0;
  1991. return 0;
  1992. }
  1993. void
  1994. qla2xxx_wake_dpc(scsi_qla_host_t *ha)
  1995. {
  1996. if (ha->dpc_thread)
  1997. wake_up_process(ha->dpc_thread);
  1998. }
  1999. /*
  2000. * qla2x00_rst_aen
  2001. * Processes asynchronous reset.
  2002. *
  2003. * Input:
  2004. * ha = adapter block pointer.
  2005. */
  2006. static void
  2007. qla2x00_rst_aen(scsi_qla_host_t *ha)
  2008. {
  2009. if (ha->flags.online && !ha->flags.reset_active &&
  2010. !atomic_read(&ha->loop_down_timer) &&
  2011. !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags))) {
  2012. do {
  2013. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  2014. /*
  2015. * Issue marker command only when we are going to start
  2016. * the I/O.
  2017. */
  2018. ha->marker_needed = 1;
  2019. } while (!atomic_read(&ha->loop_down_timer) &&
  2020. (test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags)));
  2021. }
  2022. }
  2023. static void
  2024. qla2x00_sp_free_dma(scsi_qla_host_t *ha, srb_t *sp)
  2025. {
  2026. struct scsi_cmnd *cmd = sp->cmd;
  2027. if (sp->flags & SRB_DMA_VALID) {
  2028. if (cmd->use_sg) {
  2029. dma_unmap_sg(&ha->pdev->dev, cmd->request_buffer,
  2030. cmd->use_sg, cmd->sc_data_direction);
  2031. } else if (cmd->request_bufflen) {
  2032. dma_unmap_single(&ha->pdev->dev, sp->dma_handle,
  2033. cmd->request_bufflen, cmd->sc_data_direction);
  2034. }
  2035. sp->flags &= ~SRB_DMA_VALID;
  2036. }
  2037. CMD_SP(cmd) = NULL;
  2038. }
  2039. void
  2040. qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *sp)
  2041. {
  2042. struct scsi_cmnd *cmd = sp->cmd;
  2043. qla2x00_sp_free_dma(ha, sp);
  2044. mempool_free(sp, ha->srb_mempool);
  2045. cmd->scsi_done(cmd);
  2046. }
  2047. /**************************************************************************
  2048. * qla2x00_timer
  2049. *
  2050. * Description:
  2051. * One second timer
  2052. *
  2053. * Context: Interrupt
  2054. ***************************************************************************/
  2055. static void
  2056. qla2x00_timer(scsi_qla_host_t *ha)
  2057. {
  2058. unsigned long cpu_flags = 0;
  2059. fc_port_t *fcport;
  2060. int start_dpc = 0;
  2061. int index;
  2062. srb_t *sp;
  2063. int t;
  2064. /*
  2065. * Ports - Port down timer.
  2066. *
  2067. * Whenever, a port is in the LOST state we start decrementing its port
  2068. * down timer every second until it reaches zero. Once it reaches zero
  2069. * the port it marked DEAD.
  2070. */
  2071. t = 0;
  2072. list_for_each_entry(fcport, &ha->fcports, list) {
  2073. if (fcport->port_type != FCT_TARGET)
  2074. continue;
  2075. if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
  2076. if (atomic_read(&fcport->port_down_timer) == 0)
  2077. continue;
  2078. if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
  2079. atomic_set(&fcport->state, FCS_DEVICE_DEAD);
  2080. DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
  2081. "%d remaining\n",
  2082. ha->host_no,
  2083. t, atomic_read(&fcport->port_down_timer)));
  2084. }
  2085. t++;
  2086. } /* End of for fcport */
  2087. /* Loop down handler. */
  2088. if (atomic_read(&ha->loop_down_timer) > 0 &&
  2089. !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) && ha->flags.online) {
  2090. if (atomic_read(&ha->loop_down_timer) ==
  2091. ha->loop_down_abort_time) {
  2092. DEBUG(printk("scsi(%ld): Loop Down - aborting the "
  2093. "queues before time expire\n",
  2094. ha->host_no));
  2095. if (!IS_QLA2100(ha) && ha->link_down_timeout)
  2096. atomic_set(&ha->loop_state, LOOP_DEAD);
  2097. /* Schedule an ISP abort to return any tape commands. */
  2098. spin_lock_irqsave(&ha->hardware_lock, cpu_flags);
  2099. for (index = 1; index < MAX_OUTSTANDING_COMMANDS;
  2100. index++) {
  2101. fc_port_t *sfcp;
  2102. sp = ha->outstanding_cmds[index];
  2103. if (!sp)
  2104. continue;
  2105. sfcp = sp->fcport;
  2106. if (!(sfcp->flags & FCF_TAPE_PRESENT))
  2107. continue;
  2108. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  2109. break;
  2110. }
  2111. spin_unlock_irqrestore(&ha->hardware_lock, cpu_flags);
  2112. set_bit(ABORT_QUEUES_NEEDED, &ha->dpc_flags);
  2113. start_dpc++;
  2114. }
  2115. /* if the loop has been down for 4 minutes, reinit adapter */
  2116. if (atomic_dec_and_test(&ha->loop_down_timer) != 0) {
  2117. DEBUG(printk("scsi(%ld): Loop down exceed 4 mins - "
  2118. "restarting queues.\n",
  2119. ha->host_no));
  2120. set_bit(RESTART_QUEUES_NEEDED, &ha->dpc_flags);
  2121. start_dpc++;
  2122. if (!(ha->device_flags & DFLG_NO_CABLE)) {
  2123. DEBUG(printk("scsi(%ld): Loop down - "
  2124. "aborting ISP.\n",
  2125. ha->host_no));
  2126. qla_printk(KERN_WARNING, ha,
  2127. "Loop down - aborting ISP.\n");
  2128. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  2129. }
  2130. }
  2131. DEBUG3(printk("scsi(%ld): Loop Down - seconds remaining %d\n",
  2132. ha->host_no,
  2133. atomic_read(&ha->loop_down_timer)));
  2134. }
  2135. /* Check if beacon LED needs to be blinked */
  2136. if (ha->beacon_blink_led == 1) {
  2137. set_bit(BEACON_BLINK_NEEDED, &ha->dpc_flags);
  2138. start_dpc++;
  2139. }
  2140. /* Schedule the DPC routine if needed */
  2141. if ((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) ||
  2142. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) ||
  2143. test_bit(LOOP_RESET_NEEDED, &ha->dpc_flags) ||
  2144. test_bit(FCPORT_UPDATE_NEEDED, &ha->dpc_flags) ||
  2145. start_dpc ||
  2146. test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags) ||
  2147. test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) ||
  2148. test_bit(BEACON_BLINK_NEEDED, &ha->dpc_flags) ||
  2149. test_bit(RELOGIN_NEEDED, &ha->dpc_flags)))
  2150. qla2xxx_wake_dpc(ha);
  2151. qla2x00_restart_timer(ha, WATCH_INTERVAL);
  2152. }
  2153. /* XXX(hch): crude hack to emulate a down_timeout() */
  2154. int
  2155. qla2x00_down_timeout(struct semaphore *sema, unsigned long timeout)
  2156. {
  2157. const unsigned int step = 100; /* msecs */
  2158. unsigned int iterations = jiffies_to_msecs(timeout)/100;
  2159. do {
  2160. if (!down_trylock(sema))
  2161. return 0;
  2162. if (msleep_interruptible(step))
  2163. break;
  2164. } while (--iterations >= 0);
  2165. return -ETIMEDOUT;
  2166. }
  2167. /* Firmware interface routines. */
  2168. #define FW_BLOBS 5
  2169. #define FW_ISP21XX 0
  2170. #define FW_ISP22XX 1
  2171. #define FW_ISP2300 2
  2172. #define FW_ISP2322 3
  2173. #define FW_ISP24XX 4
  2174. #define FW_FILE_ISP21XX "ql2100_fw.bin"
  2175. #define FW_FILE_ISP22XX "ql2200_fw.bin"
  2176. #define FW_FILE_ISP2300 "ql2300_fw.bin"
  2177. #define FW_FILE_ISP2322 "ql2322_fw.bin"
  2178. #define FW_FILE_ISP24XX "ql2400_fw.bin"
  2179. static DECLARE_MUTEX(qla_fw_lock);
  2180. static struct fw_blob qla_fw_blobs[FW_BLOBS] = {
  2181. { .name = FW_FILE_ISP21XX, .segs = { 0x1000, 0 }, },
  2182. { .name = FW_FILE_ISP22XX, .segs = { 0x1000, 0 }, },
  2183. { .name = FW_FILE_ISP2300, .segs = { 0x800, 0 }, },
  2184. { .name = FW_FILE_ISP2322, .segs = { 0x800, 0x1c000, 0x1e000, 0 }, },
  2185. { .name = FW_FILE_ISP24XX, },
  2186. };
  2187. struct fw_blob *
  2188. qla2x00_request_firmware(scsi_qla_host_t *ha)
  2189. {
  2190. struct fw_blob *blob;
  2191. blob = NULL;
  2192. if (IS_QLA2100(ha)) {
  2193. blob = &qla_fw_blobs[FW_ISP21XX];
  2194. } else if (IS_QLA2200(ha)) {
  2195. blob = &qla_fw_blobs[FW_ISP22XX];
  2196. } else if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
  2197. blob = &qla_fw_blobs[FW_ISP2300];
  2198. } else if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  2199. blob = &qla_fw_blobs[FW_ISP2322];
  2200. } else if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
  2201. blob = &qla_fw_blobs[FW_ISP24XX];
  2202. }
  2203. down(&qla_fw_lock);
  2204. if (blob->fw)
  2205. goto out;
  2206. if (request_firmware(&blob->fw, blob->name, &ha->pdev->dev)) {
  2207. DEBUG2(printk("scsi(%ld): Failed to load firmware image "
  2208. "(%s).\n", ha->host_no, blob->name));
  2209. blob->fw = NULL;
  2210. blob = NULL;
  2211. goto out;
  2212. }
  2213. out:
  2214. up(&qla_fw_lock);
  2215. return blob;
  2216. }
  2217. static void
  2218. qla2x00_release_firmware(void)
  2219. {
  2220. int idx;
  2221. down(&qla_fw_lock);
  2222. for (idx = 0; idx < FW_BLOBS; idx++)
  2223. if (qla_fw_blobs[idx].fw)
  2224. release_firmware(qla_fw_blobs[idx].fw);
  2225. up(&qla_fw_lock);
  2226. }
  2227. static struct pci_device_id qla2xxx_pci_tbl[] = {
  2228. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2100) },
  2229. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2200) },
  2230. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2300) },
  2231. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2312) },
  2232. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2322) },
  2233. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6312) },
  2234. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6322) },
  2235. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2422) },
  2236. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2432) },
  2237. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5422) },
  2238. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5432) },
  2239. { 0 },
  2240. };
  2241. MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
  2242. static struct pci_driver qla2xxx_pci_driver = {
  2243. .name = QLA2XXX_DRIVER_NAME,
  2244. .driver = {
  2245. .owner = THIS_MODULE,
  2246. },
  2247. .id_table = qla2xxx_pci_tbl,
  2248. .probe = qla2x00_probe_one,
  2249. .remove = __devexit_p(qla2x00_remove_one),
  2250. };
  2251. /**
  2252. * qla2x00_module_init - Module initialization.
  2253. **/
  2254. static int __init
  2255. qla2x00_module_init(void)
  2256. {
  2257. int ret = 0;
  2258. /* Allocate cache for SRBs. */
  2259. srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
  2260. SLAB_HWCACHE_ALIGN, NULL, NULL);
  2261. if (srb_cachep == NULL) {
  2262. printk(KERN_ERR
  2263. "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
  2264. return -ENOMEM;
  2265. }
  2266. /* Derive version string. */
  2267. strcpy(qla2x00_version_str, QLA2XXX_VERSION);
  2268. if (ql2xextended_error_logging)
  2269. strcat(qla2x00_version_str, "-debug");
  2270. qla2xxx_transport_template =
  2271. fc_attach_transport(&qla2xxx_transport_functions);
  2272. if (!qla2xxx_transport_template)
  2273. return -ENODEV;
  2274. printk(KERN_INFO "QLogic Fibre Channel HBA Driver\n");
  2275. ret = pci_register_driver(&qla2xxx_pci_driver);
  2276. if (ret) {
  2277. kmem_cache_destroy(srb_cachep);
  2278. fc_release_transport(qla2xxx_transport_template);
  2279. }
  2280. return ret;
  2281. }
  2282. /**
  2283. * qla2x00_module_exit - Module cleanup.
  2284. **/
  2285. static void __exit
  2286. qla2x00_module_exit(void)
  2287. {
  2288. pci_unregister_driver(&qla2xxx_pci_driver);
  2289. qla2x00_release_firmware();
  2290. kmem_cache_destroy(srb_cachep);
  2291. fc_release_transport(qla2xxx_transport_template);
  2292. }
  2293. module_init(qla2x00_module_init);
  2294. module_exit(qla2x00_module_exit);
  2295. MODULE_AUTHOR("QLogic Corporation");
  2296. MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
  2297. MODULE_LICENSE("GPL");
  2298. MODULE_VERSION(QLA2XXX_VERSION);
  2299. MODULE_FIRMWARE(FW_FILE_ISP21XX);
  2300. MODULE_FIRMWARE(FW_FILE_ISP22XX);
  2301. MODULE_FIRMWARE(FW_FILE_ISP2300);
  2302. MODULE_FIRMWARE(FW_FILE_ISP2322);
  2303. MODULE_FIRMWARE(FW_FILE_ISP24XX);