qla_os.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316
  1. /*
  2. * QLOGIC LINUX SOFTWARE
  3. *
  4. * QLogic ISP2x00 device driver for Linux 2.6.x
  5. * Copyright (C) 2003-2004 QLogic Corporation
  6. * (www.qlogic.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2, or (at your option) any
  11. * later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. */
  19. #include "qla_def.h"
  20. #include <linux/moduleparam.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/smp_lock.h>
  23. #include <linux/delay.h>
  24. #include <scsi/scsi_tcq.h>
  25. #include <scsi/scsicam.h>
  26. #include <scsi/scsi_transport.h>
  27. #include <scsi/scsi_transport_fc.h>
  28. /*
  29. * Driver version
  30. */
  31. char qla2x00_version_str[40];
  32. /*
  33. * SRB allocation cache
  34. */
  35. static kmem_cache_t *srb_cachep;
  36. /*
  37. * Ioctl related information.
  38. */
  39. static int num_hosts;
  40. int ql2xlogintimeout = 20;
  41. module_param(ql2xlogintimeout, int, S_IRUGO|S_IRUSR);
  42. MODULE_PARM_DESC(ql2xlogintimeout,
  43. "Login timeout value in seconds.");
  44. int qlport_down_retry = 30;
  45. module_param(qlport_down_retry, int, S_IRUGO|S_IRUSR);
  46. MODULE_PARM_DESC(qlport_down_retry,
  47. "Maximum number of command retries to a port that returns"
  48. "a PORT-DOWN status.");
  49. int ql2xplogiabsentdevice;
  50. module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
  51. MODULE_PARM_DESC(ql2xplogiabsentdevice,
  52. "Option to enable PLOGI to devices that are not present after "
  53. "a Fabric scan. This is needed for several broken switches."
  54. "Default is 0 - no PLOGI. 1 - perfom PLOGI.");
  55. int ql2xenablezio = 0;
  56. module_param(ql2xenablezio, int, S_IRUGO|S_IRUSR);
  57. MODULE_PARM_DESC(ql2xenablezio,
  58. "Option to enable ZIO:If 1 then enable it otherwise"
  59. " use the default set in the NVRAM."
  60. " Default is 0 : disabled");
  61. int ql2xintrdelaytimer = 10;
  62. module_param(ql2xintrdelaytimer, int, S_IRUGO|S_IRUSR);
  63. MODULE_PARM_DESC(ql2xintrdelaytimer,
  64. "ZIO: Waiting time for Firmware before it generates an "
  65. "interrupt to the host to notify completion of request.");
  66. int ql2xloginretrycount = 0;
  67. module_param(ql2xloginretrycount, int, S_IRUGO|S_IRUSR);
  68. MODULE_PARM_DESC(ql2xloginretrycount,
  69. "Specify an alternate value for the NVRAM login retry count.");
  70. static void qla2x00_free_device(scsi_qla_host_t *);
  71. static void qla2x00_config_dma_addressing(scsi_qla_host_t *ha);
  72. /*
  73. * SCSI host template entry points
  74. */
  75. static int qla2xxx_slave_configure(struct scsi_device * device);
  76. static int qla2xxx_slave_alloc(struct scsi_device *);
  77. static void qla2xxx_slave_destroy(struct scsi_device *);
  78. static int qla2x00_queuecommand(struct scsi_cmnd *cmd,
  79. void (*fn)(struct scsi_cmnd *));
  80. static int qla2xxx_eh_abort(struct scsi_cmnd *);
  81. static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
  82. static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
  83. static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
  84. static int qla2x00_loop_reset(scsi_qla_host_t *ha);
  85. static int qla2x00_device_reset(scsi_qla_host_t *, fc_port_t *);
  86. static struct scsi_host_template qla2x00_driver_template = {
  87. .module = THIS_MODULE,
  88. .name = "qla2xxx",
  89. .queuecommand = qla2x00_queuecommand,
  90. .eh_abort_handler = qla2xxx_eh_abort,
  91. .eh_device_reset_handler = qla2xxx_eh_device_reset,
  92. .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
  93. .eh_host_reset_handler = qla2xxx_eh_host_reset,
  94. .slave_configure = qla2xxx_slave_configure,
  95. .slave_alloc = qla2xxx_slave_alloc,
  96. .slave_destroy = qla2xxx_slave_destroy,
  97. .this_id = -1,
  98. .cmd_per_lun = 3,
  99. .use_clustering = ENABLE_CLUSTERING,
  100. .sg_tablesize = SG_ALL,
  101. /*
  102. * The RISC allows for each command to transfer (2^32-1) bytes of data,
  103. * which equates to 0x800000 sectors.
  104. */
  105. .max_sectors = 0xFFFF,
  106. };
  107. static struct scsi_transport_template *qla2xxx_transport_template = NULL;
  108. /* TODO Convert to inlines
  109. *
  110. * Timer routines
  111. */
  112. #define WATCH_INTERVAL 1 /* number of seconds */
  113. static void qla2x00_timer(scsi_qla_host_t *);
  114. static __inline__ void qla2x00_start_timer(scsi_qla_host_t *,
  115. void *, unsigned long);
  116. static __inline__ void qla2x00_restart_timer(scsi_qla_host_t *, unsigned long);
  117. static __inline__ void qla2x00_stop_timer(scsi_qla_host_t *);
  118. static inline void
  119. qla2x00_start_timer(scsi_qla_host_t *ha, void *func, unsigned long interval)
  120. {
  121. init_timer(&ha->timer);
  122. ha->timer.expires = jiffies + interval * HZ;
  123. ha->timer.data = (unsigned long)ha;
  124. ha->timer.function = (void (*)(unsigned long))func;
  125. add_timer(&ha->timer);
  126. ha->timer_active = 1;
  127. }
  128. static inline void
  129. qla2x00_restart_timer(scsi_qla_host_t *ha, unsigned long interval)
  130. {
  131. mod_timer(&ha->timer, jiffies + interval * HZ);
  132. }
  133. static __inline__ void
  134. qla2x00_stop_timer(scsi_qla_host_t *ha)
  135. {
  136. del_timer_sync(&ha->timer);
  137. ha->timer_active = 0;
  138. }
  139. static int qla2x00_do_dpc(void *data);
  140. static void qla2x00_rst_aen(scsi_qla_host_t *);
  141. static uint8_t qla2x00_mem_alloc(scsi_qla_host_t *);
  142. static void qla2x00_mem_free(scsi_qla_host_t *ha);
  143. static int qla2x00_allocate_sp_pool( scsi_qla_host_t *ha);
  144. static void qla2x00_free_sp_pool(scsi_qla_host_t *ha);
  145. static srb_t *qla2x00_get_new_sp(scsi_qla_host_t *);
  146. static void qla2x00_sp_free_dma(scsi_qla_host_t *, srb_t *);
  147. void qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *);
  148. /* -------------------------------------------------------------------------- */
  149. static char *
  150. qla2x00_get_pci_info_str(struct scsi_qla_host *ha, char *str)
  151. {
  152. static char *pci_bus_modes[] = {
  153. "33", "66", "100", "133",
  154. };
  155. uint16_t pci_bus;
  156. strcpy(str, "PCI");
  157. pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
  158. if (pci_bus) {
  159. strcat(str, "-X (");
  160. strcat(str, pci_bus_modes[pci_bus]);
  161. } else {
  162. pci_bus = (ha->pci_attr & BIT_8) >> 8;
  163. strcat(str, " (");
  164. strcat(str, pci_bus_modes[pci_bus]);
  165. }
  166. strcat(str, " MHz)");
  167. return (str);
  168. }
  169. char *
  170. qla2x00_get_fw_version_str(struct scsi_qla_host *ha, char *str)
  171. {
  172. char un_str[10];
  173. sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
  174. ha->fw_minor_version,
  175. ha->fw_subminor_version);
  176. if (ha->fw_attributes & BIT_9) {
  177. strcat(str, "FLX");
  178. return (str);
  179. }
  180. switch (ha->fw_attributes & 0xFF) {
  181. case 0x7:
  182. strcat(str, "EF");
  183. break;
  184. case 0x17:
  185. strcat(str, "TP");
  186. break;
  187. case 0x37:
  188. strcat(str, "IP");
  189. break;
  190. case 0x77:
  191. strcat(str, "VI");
  192. break;
  193. default:
  194. sprintf(un_str, "(%x)", ha->fw_attributes);
  195. strcat(str, un_str);
  196. break;
  197. }
  198. if (ha->fw_attributes & 0x100)
  199. strcat(str, "X");
  200. return (str);
  201. }
  202. /**************************************************************************
  203. * qla2x00_queuecommand
  204. *
  205. * Description:
  206. * Queue a command to the controller.
  207. *
  208. * Input:
  209. * cmd - pointer to Scsi cmd structure
  210. * fn - pointer to Scsi done function
  211. *
  212. * Returns:
  213. * 0 - Always
  214. *
  215. * Note:
  216. * The mid-level driver tries to ensures that queuecommand never gets invoked
  217. * concurrently with itself or the interrupt handler (although the
  218. * interrupt handler may call this routine as part of request-completion
  219. * handling).
  220. **************************************************************************/
  221. static int
  222. qla2x00_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  223. {
  224. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  225. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  226. srb_t *sp;
  227. int rval;
  228. if (!fcport) {
  229. cmd->result = DID_NO_CONNECT << 16;
  230. goto qc_fail_command;
  231. }
  232. if (atomic_read(&fcport->state) != FCS_ONLINE) {
  233. if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
  234. atomic_read(&ha->loop_state) == LOOP_DEAD) {
  235. cmd->result = DID_NO_CONNECT << 16;
  236. goto qc_fail_command;
  237. }
  238. goto qc_host_busy;
  239. }
  240. spin_unlock_irq(ha->host->host_lock);
  241. /* Allocate a command packet from the "sp" pool. */
  242. if ((sp = qla2x00_get_new_sp(ha)) == NULL) {
  243. goto qc_host_busy_lock;
  244. }
  245. sp->ha = ha;
  246. sp->fcport = fcport;
  247. sp->cmd = cmd;
  248. sp->flags = 0;
  249. CMD_SP(cmd) = (void *)sp;
  250. cmd->scsi_done = done;
  251. rval = qla2x00_start_scsi(sp);
  252. if (rval != QLA_SUCCESS)
  253. goto qc_host_busy_free_sp;
  254. /* Manage unprocessed RIO/ZIO commands in response queue. */
  255. if (ha->flags.online && ha->flags.process_response_queue &&
  256. ha->response_ring_ptr->signature != RESPONSE_PROCESSED) {
  257. unsigned long flags;
  258. spin_lock_irqsave(&ha->hardware_lock, flags);
  259. qla2x00_process_response_queue(ha);
  260. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  261. }
  262. spin_lock_irq(ha->host->host_lock);
  263. return 0;
  264. qc_host_busy_free_sp:
  265. qla2x00_sp_free_dma(ha, sp);
  266. CMD_SP(cmd) = NULL;
  267. mempool_free(sp, ha->srb_mempool);
  268. qc_host_busy_lock:
  269. spin_lock_irq(ha->host->host_lock);
  270. qc_host_busy:
  271. return SCSI_MLQUEUE_HOST_BUSY;
  272. qc_fail_command:
  273. done(cmd);
  274. return 0;
  275. }
  276. /*
  277. * qla2x00_eh_wait_on_command
  278. * Waits for the command to be returned by the Firmware for some
  279. * max time.
  280. *
  281. * Input:
  282. * ha = actual ha whose done queue will contain the command
  283. * returned by firmware.
  284. * cmd = Scsi Command to wait on.
  285. * flag = Abort/Reset(Bus or Device Reset)
  286. *
  287. * Return:
  288. * Not Found : 0
  289. * Found : 1
  290. */
  291. static int
  292. qla2x00_eh_wait_on_command(scsi_qla_host_t *ha, struct scsi_cmnd *cmd)
  293. {
  294. #define ABORT_POLLING_PERIOD HZ
  295. #define ABORT_WAIT_ITER ((10 * HZ) / (ABORT_POLLING_PERIOD))
  296. unsigned long wait_iter = ABORT_WAIT_ITER;
  297. int ret = QLA_SUCCESS;
  298. while (CMD_SP(cmd)) {
  299. set_current_state(TASK_UNINTERRUPTIBLE);
  300. schedule_timeout(ABORT_POLLING_PERIOD);
  301. if (--wait_iter)
  302. break;
  303. }
  304. if (CMD_SP(cmd))
  305. ret = QLA_FUNCTION_FAILED;
  306. return ret;
  307. }
  308. /*
  309. * qla2x00_wait_for_hba_online
  310. * Wait till the HBA is online after going through
  311. * <= MAX_RETRIES_OF_ISP_ABORT or
  312. * finally HBA is disabled ie marked offline
  313. *
  314. * Input:
  315. * ha - pointer to host adapter structure
  316. *
  317. * Note:
  318. * Does context switching-Release SPIN_LOCK
  319. * (if any) before calling this routine.
  320. *
  321. * Return:
  322. * Success (Adapter is online) : 0
  323. * Failed (Adapter is offline/disabled) : 1
  324. */
  325. static int
  326. qla2x00_wait_for_hba_online(scsi_qla_host_t *ha)
  327. {
  328. int return_status;
  329. unsigned long wait_online;
  330. wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
  331. while (((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) ||
  332. test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
  333. test_bit(ISP_ABORT_RETRY, &ha->dpc_flags) ||
  334. ha->dpc_active) && time_before(jiffies, wait_online)) {
  335. msleep(1000);
  336. }
  337. if (ha->flags.online)
  338. return_status = QLA_SUCCESS;
  339. else
  340. return_status = QLA_FUNCTION_FAILED;
  341. DEBUG2(printk("%s return_status=%d\n",__func__,return_status));
  342. return (return_status);
  343. }
  344. /*
  345. * qla2x00_wait_for_loop_ready
  346. * Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
  347. * to be in LOOP_READY state.
  348. * Input:
  349. * ha - pointer to host adapter structure
  350. *
  351. * Note:
  352. * Does context switching-Release SPIN_LOCK
  353. * (if any) before calling this routine.
  354. *
  355. *
  356. * Return:
  357. * Success (LOOP_READY) : 0
  358. * Failed (LOOP_NOT_READY) : 1
  359. */
  360. static inline int
  361. qla2x00_wait_for_loop_ready(scsi_qla_host_t *ha)
  362. {
  363. int return_status = QLA_SUCCESS;
  364. unsigned long loop_timeout ;
  365. /* wait for 5 min at the max for loop to be ready */
  366. loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ);
  367. while ((!atomic_read(&ha->loop_down_timer) &&
  368. atomic_read(&ha->loop_state) == LOOP_DOWN) ||
  369. atomic_read(&ha->loop_state) != LOOP_READY) {
  370. msleep(1000);
  371. if (time_after_eq(jiffies, loop_timeout)) {
  372. return_status = QLA_FUNCTION_FAILED;
  373. break;
  374. }
  375. }
  376. return (return_status);
  377. }
  378. /**************************************************************************
  379. * qla2xxx_eh_abort
  380. *
  381. * Description:
  382. * The abort function will abort the specified command.
  383. *
  384. * Input:
  385. * cmd = Linux SCSI command packet to be aborted.
  386. *
  387. * Returns:
  388. * Either SUCCESS or FAILED.
  389. *
  390. * Note:
  391. **************************************************************************/
  392. int
  393. qla2xxx_eh_abort(struct scsi_cmnd *cmd)
  394. {
  395. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  396. srb_t *sp;
  397. int ret, i;
  398. unsigned int id, lun;
  399. unsigned long serial;
  400. if (!CMD_SP(cmd))
  401. return FAILED;
  402. ret = FAILED;
  403. id = cmd->device->id;
  404. lun = cmd->device->lun;
  405. serial = cmd->serial_number;
  406. /* Check active list for command command. */
  407. spin_lock(&ha->hardware_lock);
  408. for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
  409. sp = ha->outstanding_cmds[i];
  410. if (sp == NULL)
  411. continue;
  412. if (sp->cmd != cmd)
  413. continue;
  414. DEBUG2(printk("%s(%ld): aborting sp %p from RISC. pid=%ld "
  415. "sp->state=%x\n", __func__, ha->host_no, sp, serial,
  416. sp->state));
  417. DEBUG3(qla2x00_print_scsi_cmd(cmd);)
  418. spin_unlock(&ha->hardware_lock);
  419. if (qla2x00_abort_command(ha, sp)) {
  420. DEBUG2(printk("%s(%ld): abort_command "
  421. "mbx failed.\n", __func__, ha->host_no));
  422. } else {
  423. DEBUG3(printk("%s(%ld): abort_command "
  424. "mbx success.\n", __func__, ha->host_no));
  425. ret = SUCCESS;
  426. }
  427. spin_lock(&ha->hardware_lock);
  428. break;
  429. }
  430. /* Wait for the command to be returned. */
  431. if (ret == SUCCESS) {
  432. spin_unlock(&ha->hardware_lock);
  433. if (qla2x00_eh_wait_on_command(ha, cmd) != QLA_SUCCESS) {
  434. qla_printk(KERN_ERR, ha,
  435. "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
  436. "%x.\n", ha->host_no, id, lun, serial, ret);
  437. }
  438. spin_lock(&ha->hardware_lock);
  439. }
  440. qla_printk(KERN_INFO, ha,
  441. "scsi(%ld:%d:%d): Abort command issued -- %lx %x.\n", ha->host_no,
  442. id, lun, serial, ret);
  443. return ret;
  444. }
  445. /**************************************************************************
  446. * qla2x00_eh_wait_for_pending_target_commands
  447. *
  448. * Description:
  449. * Waits for all the commands to come back from the specified target.
  450. *
  451. * Input:
  452. * ha - pointer to scsi_qla_host structure.
  453. * t - target
  454. * Returns:
  455. * Either SUCCESS or FAILED.
  456. *
  457. * Note:
  458. **************************************************************************/
  459. static int
  460. qla2x00_eh_wait_for_pending_target_commands(scsi_qla_host_t *ha, unsigned int t)
  461. {
  462. int cnt;
  463. int status;
  464. srb_t *sp;
  465. struct scsi_cmnd *cmd;
  466. status = 0;
  467. /*
  468. * Waiting for all commands for the designated target in the active
  469. * array
  470. */
  471. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  472. spin_lock(&ha->hardware_lock);
  473. sp = ha->outstanding_cmds[cnt];
  474. if (sp) {
  475. cmd = sp->cmd;
  476. spin_unlock(&ha->hardware_lock);
  477. if (cmd->device->id == t) {
  478. if (!qla2x00_eh_wait_on_command(ha, cmd)) {
  479. status = 1;
  480. break;
  481. }
  482. }
  483. } else {
  484. spin_unlock(&ha->hardware_lock);
  485. }
  486. }
  487. return (status);
  488. }
  489. /**************************************************************************
  490. * qla2xxx_eh_device_reset
  491. *
  492. * Description:
  493. * The device reset function will reset the target and abort any
  494. * executing commands.
  495. *
  496. * NOTE: The use of SP is undefined within this context. Do *NOT*
  497. * attempt to use this value, even if you determine it is
  498. * non-null.
  499. *
  500. * Input:
  501. * cmd = Linux SCSI command packet of the command that cause the
  502. * bus device reset.
  503. *
  504. * Returns:
  505. * SUCCESS/FAILURE (defined as macro in scsi.h).
  506. *
  507. **************************************************************************/
  508. int
  509. qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
  510. {
  511. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  512. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  513. srb_t *sp;
  514. int ret;
  515. unsigned int id, lun;
  516. unsigned long serial;
  517. ret = FAILED;
  518. id = cmd->device->id;
  519. lun = cmd->device->lun;
  520. serial = cmd->serial_number;
  521. sp = (srb_t *) CMD_SP(cmd);
  522. if (!sp || !fcport)
  523. return ret;
  524. qla_printk(KERN_INFO, ha,
  525. "scsi(%ld:%d:%d): DEVICE RESET ISSUED.\n", ha->host_no, id, lun);
  526. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  527. goto eh_dev_reset_done;
  528. if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
  529. if (qla2x00_device_reset(ha, fcport) == 0)
  530. ret = SUCCESS;
  531. #if defined(LOGOUT_AFTER_DEVICE_RESET)
  532. if (ret == SUCCESS) {
  533. if (fcport->flags & FC_FABRIC_DEVICE) {
  534. qla2x00_fabric_logout(ha, fcport->loop_id);
  535. qla2x00_mark_device_lost(ha, fcport);
  536. }
  537. }
  538. #endif
  539. } else {
  540. DEBUG2(printk(KERN_INFO
  541. "%s failed: loop not ready\n",__func__);)
  542. }
  543. if (ret == FAILED) {
  544. DEBUG3(printk("%s(%ld): device reset failed\n",
  545. __func__, ha->host_no));
  546. qla_printk(KERN_INFO, ha, "%s: device reset failed\n",
  547. __func__);
  548. goto eh_dev_reset_done;
  549. }
  550. /*
  551. * If we are coming down the EH path, wait for all commands to
  552. * complete for the device.
  553. */
  554. if (cmd->device->host->eh_active) {
  555. if (qla2x00_eh_wait_for_pending_target_commands(ha, id))
  556. ret = FAILED;
  557. if (ret == FAILED) {
  558. DEBUG3(printk("%s(%ld): failed while waiting for "
  559. "commands\n", __func__, ha->host_no));
  560. qla_printk(KERN_INFO, ha,
  561. "%s: failed while waiting for commands\n",
  562. __func__);
  563. goto eh_dev_reset_done;
  564. }
  565. }
  566. qla_printk(KERN_INFO, ha,
  567. "scsi(%ld:%d:%d): DEVICE RESET SUCCEEDED.\n", ha->host_no, id, lun);
  568. eh_dev_reset_done:
  569. return ret;
  570. }
  571. /**************************************************************************
  572. * qla2x00_eh_wait_for_pending_commands
  573. *
  574. * Description:
  575. * Waits for all the commands to come back from the specified host.
  576. *
  577. * Input:
  578. * ha - pointer to scsi_qla_host structure.
  579. *
  580. * Returns:
  581. * 1 : SUCCESS
  582. * 0 : FAILED
  583. *
  584. * Note:
  585. **************************************************************************/
  586. static int
  587. qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *ha)
  588. {
  589. int cnt;
  590. int status;
  591. srb_t *sp;
  592. struct scsi_cmnd *cmd;
  593. status = 1;
  594. /*
  595. * Waiting for all commands for the designated target in the active
  596. * array
  597. */
  598. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  599. spin_lock(&ha->hardware_lock);
  600. sp = ha->outstanding_cmds[cnt];
  601. if (sp) {
  602. cmd = sp->cmd;
  603. spin_unlock(&ha->hardware_lock);
  604. status = qla2x00_eh_wait_on_command(ha, cmd);
  605. if (status == 0)
  606. break;
  607. }
  608. else {
  609. spin_unlock(&ha->hardware_lock);
  610. }
  611. }
  612. return (status);
  613. }
  614. /**************************************************************************
  615. * qla2xxx_eh_bus_reset
  616. *
  617. * Description:
  618. * The bus reset function will reset the bus and abort any executing
  619. * commands.
  620. *
  621. * Input:
  622. * cmd = Linux SCSI command packet of the command that cause the
  623. * bus reset.
  624. *
  625. * Returns:
  626. * SUCCESS/FAILURE (defined as macro in scsi.h).
  627. *
  628. **************************************************************************/
  629. int
  630. qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
  631. {
  632. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  633. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  634. srb_t *sp;
  635. int ret;
  636. unsigned int id, lun;
  637. unsigned long serial;
  638. ret = FAILED;
  639. id = cmd->device->id;
  640. lun = cmd->device->lun;
  641. serial = cmd->serial_number;
  642. sp = (srb_t *) CMD_SP(cmd);
  643. if (!sp || !fcport)
  644. return ret;
  645. qla_printk(KERN_INFO, ha,
  646. "scsi(%ld:%d:%d): LOOP RESET ISSUED.\n", ha->host_no, id, lun);
  647. spin_unlock_irq(ha->host->host_lock);
  648. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
  649. DEBUG2(printk("%s failed:board disabled\n",__func__));
  650. goto eh_bus_reset_done;
  651. }
  652. if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
  653. if (qla2x00_loop_reset(ha) == QLA_SUCCESS)
  654. ret = SUCCESS;
  655. }
  656. if (ret == FAILED)
  657. goto eh_bus_reset_done;
  658. /* Waiting for our command in done_queue to be returned to OS.*/
  659. if (cmd->device->host->eh_active)
  660. if (!qla2x00_eh_wait_for_pending_commands(ha))
  661. ret = FAILED;
  662. eh_bus_reset_done:
  663. qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
  664. (ret == FAILED) ? "failed" : "succeded");
  665. spin_lock_irq(ha->host->host_lock);
  666. return ret;
  667. }
  668. /**************************************************************************
  669. * qla2xxx_eh_host_reset
  670. *
  671. * Description:
  672. * The reset function will reset the Adapter.
  673. *
  674. * Input:
  675. * cmd = Linux SCSI command packet of the command that cause the
  676. * adapter reset.
  677. *
  678. * Returns:
  679. * Either SUCCESS or FAILED.
  680. *
  681. * Note:
  682. **************************************************************************/
  683. int
  684. qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
  685. {
  686. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  687. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  688. srb_t *sp;
  689. int ret;
  690. unsigned int id, lun;
  691. unsigned long serial;
  692. ret = FAILED;
  693. id = cmd->device->id;
  694. lun = cmd->device->lun;
  695. serial = cmd->serial_number;
  696. sp = (srb_t *) CMD_SP(cmd);
  697. if (!sp || !fcport)
  698. return ret;
  699. qla_printk(KERN_INFO, ha,
  700. "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no, id, lun);
  701. spin_unlock_irq(ha->host->host_lock);
  702. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  703. goto eh_host_reset_lock;
  704. /*
  705. * Fixme-may be dpc thread is active and processing
  706. * loop_resync,so wait a while for it to
  707. * be completed and then issue big hammer.Otherwise
  708. * it may cause I/O failure as big hammer marks the
  709. * devices as lost kicking of the port_down_timer
  710. * while dpc is stuck for the mailbox to complete.
  711. */
  712. qla2x00_wait_for_loop_ready(ha);
  713. set_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  714. if (qla2x00_abort_isp(ha)) {
  715. clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  716. /* failed. schedule dpc to try */
  717. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  718. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  719. goto eh_host_reset_lock;
  720. }
  721. clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  722. /* Waiting for our command in done_queue to be returned to OS.*/
  723. if (qla2x00_eh_wait_for_pending_commands(ha))
  724. ret = SUCCESS;
  725. eh_host_reset_lock:
  726. spin_lock_irq(ha->host->host_lock);
  727. qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
  728. (ret == FAILED) ? "failed" : "succeded");
  729. return ret;
  730. }
  731. /*
  732. * qla2x00_loop_reset
  733. * Issue loop reset.
  734. *
  735. * Input:
  736. * ha = adapter block pointer.
  737. *
  738. * Returns:
  739. * 0 = success
  740. */
  741. static int
  742. qla2x00_loop_reset(scsi_qla_host_t *ha)
  743. {
  744. int status = QLA_SUCCESS;
  745. struct fc_port *fcport;
  746. if (ha->flags.enable_lip_reset) {
  747. status = qla2x00_lip_reset(ha);
  748. }
  749. if (status == QLA_SUCCESS && ha->flags.enable_target_reset) {
  750. list_for_each_entry(fcport, &ha->fcports, list) {
  751. if (fcport->port_type != FCT_TARGET)
  752. continue;
  753. status = qla2x00_target_reset(ha, fcport);
  754. if (status != QLA_SUCCESS)
  755. break;
  756. }
  757. }
  758. if (status == QLA_SUCCESS &&
  759. ((!ha->flags.enable_target_reset &&
  760. !ha->flags.enable_lip_reset) ||
  761. ha->flags.enable_lip_full_login)) {
  762. status = qla2x00_full_login_lip(ha);
  763. }
  764. /* Issue marker command only when we are going to start the I/O */
  765. ha->marker_needed = 1;
  766. if (status) {
  767. /* Empty */
  768. DEBUG2_3(printk("%s(%ld): **** FAILED ****\n",
  769. __func__,
  770. ha->host_no);)
  771. } else {
  772. /* Empty */
  773. DEBUG3(printk("%s(%ld): exiting normally.\n",
  774. __func__,
  775. ha->host_no);)
  776. }
  777. return(status);
  778. }
  779. /*
  780. * qla2x00_device_reset
  781. * Issue bus device reset message to the target.
  782. *
  783. * Input:
  784. * ha = adapter block pointer.
  785. * t = SCSI ID.
  786. * TARGET_QUEUE_LOCK must be released.
  787. * ADAPTER_STATE_LOCK must be released.
  788. *
  789. * Context:
  790. * Kernel context.
  791. */
  792. static int
  793. qla2x00_device_reset(scsi_qla_host_t *ha, fc_port_t *reset_fcport)
  794. {
  795. /* Abort Target command will clear Reservation */
  796. return qla2x00_abort_target(reset_fcport);
  797. }
  798. static int
  799. qla2xxx_slave_alloc(struct scsi_device *sdev)
  800. {
  801. scsi_qla_host_t *ha = to_qla_host(sdev->host);
  802. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  803. fc_port_t *fcport;
  804. int found;
  805. if (!rport)
  806. return -ENXIO;
  807. found = 0;
  808. list_for_each_entry(fcport, &ha->fcports, list) {
  809. if (rport->port_name ==
  810. be64_to_cpu(*(uint64_t *)fcport->port_name)) {
  811. found++;
  812. break;
  813. }
  814. }
  815. if (!found)
  816. return -ENXIO;
  817. sdev->hostdata = fcport;
  818. return 0;
  819. }
  820. static int
  821. qla2xxx_slave_configure(struct scsi_device *sdev)
  822. {
  823. scsi_qla_host_t *ha = to_qla_host(sdev->host);
  824. struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
  825. if (sdev->tagged_supported)
  826. scsi_activate_tcq(sdev, 32);
  827. else
  828. scsi_deactivate_tcq(sdev, 32);
  829. rport->dev_loss_tmo = ha->port_down_retry_count + 5;
  830. return 0;
  831. }
  832. static void
  833. qla2xxx_slave_destroy(struct scsi_device *sdev)
  834. {
  835. sdev->hostdata = NULL;
  836. }
  837. /**
  838. * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
  839. * @ha: HA context
  840. *
  841. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  842. * supported addressing method.
  843. */
  844. static void
  845. qla2x00_config_dma_addressing(scsi_qla_host_t *ha)
  846. {
  847. /* Assume 32bit DMA address */
  848. ha->flags.enable_64bit_addressing = 0;
  849. ha->calc_request_entries = qla2x00_calc_iocbs_32;
  850. ha->build_scsi_iocbs = qla2x00_build_scsi_iocbs_32;
  851. /*
  852. * Given the two variants pci_set_dma_mask(), allow the compiler to
  853. * assist in setting the proper dma mask.
  854. */
  855. if (sizeof(dma_addr_t) > 4) {
  856. if (pci_set_dma_mask(ha->pdev, DMA_64BIT_MASK) == 0) {
  857. ha->flags.enable_64bit_addressing = 1;
  858. ha->calc_request_entries = qla2x00_calc_iocbs_64;
  859. ha->build_scsi_iocbs = qla2x00_build_scsi_iocbs_64;
  860. if (pci_set_consistent_dma_mask(ha->pdev,
  861. DMA_64BIT_MASK)) {
  862. qla_printk(KERN_DEBUG, ha,
  863. "Failed to set 64 bit PCI consistent mask; "
  864. "using 32 bit.\n");
  865. pci_set_consistent_dma_mask(ha->pdev,
  866. DMA_32BIT_MASK);
  867. }
  868. } else {
  869. qla_printk(KERN_DEBUG, ha,
  870. "Failed to set 64 bit PCI DMA mask, falling back "
  871. "to 32 bit MASK.\n");
  872. pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
  873. }
  874. } else {
  875. pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
  876. }
  877. }
  878. static int
  879. qla2x00_iospace_config(scsi_qla_host_t *ha)
  880. {
  881. unsigned long pio, pio_len, pio_flags;
  882. unsigned long mmio, mmio_len, mmio_flags;
  883. /* We only need PIO for Flash operations on ISP2312 v2 chips. */
  884. pio = pci_resource_start(ha->pdev, 0);
  885. pio_len = pci_resource_len(ha->pdev, 0);
  886. pio_flags = pci_resource_flags(ha->pdev, 0);
  887. if (pio_flags & IORESOURCE_IO) {
  888. if (pio_len < MIN_IOBASE_LEN) {
  889. qla_printk(KERN_WARNING, ha,
  890. "Invalid PCI I/O region size (%s)...\n",
  891. pci_name(ha->pdev));
  892. pio = 0;
  893. }
  894. } else {
  895. qla_printk(KERN_WARNING, ha,
  896. "region #0 not a PIO resource (%s)...\n",
  897. pci_name(ha->pdev));
  898. pio = 0;
  899. }
  900. /* Use MMIO operations for all accesses. */
  901. mmio = pci_resource_start(ha->pdev, 1);
  902. mmio_len = pci_resource_len(ha->pdev, 1);
  903. mmio_flags = pci_resource_flags(ha->pdev, 1);
  904. if (!(mmio_flags & IORESOURCE_MEM)) {
  905. qla_printk(KERN_ERR, ha,
  906. "region #0 not an MMIO resource (%s), aborting\n",
  907. pci_name(ha->pdev));
  908. goto iospace_error_exit;
  909. }
  910. if (mmio_len < MIN_IOBASE_LEN) {
  911. qla_printk(KERN_ERR, ha,
  912. "Invalid PCI mem region size (%s), aborting\n",
  913. pci_name(ha->pdev));
  914. goto iospace_error_exit;
  915. }
  916. if (pci_request_regions(ha->pdev, ha->brd_info->drv_name)) {
  917. qla_printk(KERN_WARNING, ha,
  918. "Failed to reserve PIO/MMIO regions (%s)\n",
  919. pci_name(ha->pdev));
  920. goto iospace_error_exit;
  921. }
  922. ha->pio_address = pio;
  923. ha->pio_length = pio_len;
  924. ha->iobase = ioremap(mmio, MIN_IOBASE_LEN);
  925. if (!ha->iobase) {
  926. qla_printk(KERN_ERR, ha,
  927. "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
  928. goto iospace_error_exit;
  929. }
  930. return (0);
  931. iospace_error_exit:
  932. return (-ENOMEM);
  933. }
  934. /*
  935. * PCI driver interface
  936. */
  937. int qla2x00_probe_one(struct pci_dev *pdev, struct qla_board_info *brd_info)
  938. {
  939. int ret;
  940. device_reg_t __iomem *reg;
  941. struct Scsi_Host *host;
  942. scsi_qla_host_t *ha;
  943. unsigned long flags = 0;
  944. unsigned long wait_switch = 0;
  945. char pci_info[20];
  946. char fw_str[30];
  947. fc_port_t *fcport;
  948. if (pci_enable_device(pdev))
  949. return -1;
  950. host = scsi_host_alloc(&qla2x00_driver_template,
  951. sizeof(scsi_qla_host_t));
  952. if (host == NULL) {
  953. printk(KERN_WARNING
  954. "qla2xxx: Couldn't allocate host from scsi layer!\n");
  955. goto probe_disable_device;
  956. }
  957. /* Clear our data area */
  958. ha = (scsi_qla_host_t *)host->hostdata;
  959. memset(ha, 0, sizeof(scsi_qla_host_t));
  960. ha->pdev = pdev;
  961. ha->host = host;
  962. ha->host_no = host->host_no;
  963. ha->brd_info = brd_info;
  964. sprintf(ha->host_str, "%s_%ld", ha->brd_info->drv_name, ha->host_no);
  965. /* Configure PCI I/O space */
  966. ret = qla2x00_iospace_config(ha);
  967. if (ret != 0) {
  968. goto probe_alloc_failed;
  969. }
  970. /* Sanitize the information from PCI BIOS. */
  971. host->irq = pdev->irq;
  972. qla_printk(KERN_INFO, ha,
  973. "Found an %s, irq %d, iobase 0x%p\n", ha->brd_info->isp_name,
  974. host->irq, ha->iobase);
  975. spin_lock_init(&ha->hardware_lock);
  976. ha->prev_topology = 0;
  977. ha->ports = MAX_BUSES;
  978. if (IS_QLA2100(ha)) {
  979. host->max_id = MAX_TARGETS_2100;
  980. ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
  981. ha->request_q_length = REQUEST_ENTRY_CNT_2100;
  982. ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
  983. ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
  984. host->sg_tablesize = 32;
  985. } else if (IS_QLA2200(ha)) {
  986. host->max_id = MAX_TARGETS_2200;
  987. ha->mbx_count = MAILBOX_REGISTER_COUNT;
  988. ha->request_q_length = REQUEST_ENTRY_CNT_2200;
  989. ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
  990. ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
  991. } else /*if (IS_QLA2300(ha))*/ {
  992. host->max_id = MAX_TARGETS_2200;
  993. ha->mbx_count = MAILBOX_REGISTER_COUNT;
  994. ha->request_q_length = REQUEST_ENTRY_CNT_2200;
  995. ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
  996. ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
  997. }
  998. host->can_queue = ha->request_q_length + 128;
  999. /* load the F/W, read paramaters, and init the H/W */
  1000. ha->instance = num_hosts;
  1001. init_MUTEX(&ha->mbx_cmd_sem);
  1002. init_MUTEX_LOCKED(&ha->mbx_intr_sem);
  1003. INIT_LIST_HEAD(&ha->list);
  1004. INIT_LIST_HEAD(&ha->fcports);
  1005. INIT_LIST_HEAD(&ha->rscn_fcports);
  1006. /*
  1007. * These locks are used to prevent more than one CPU
  1008. * from modifying the queue at the same time. The
  1009. * higher level "host_lock" will reduce most
  1010. * contention for these locks.
  1011. */
  1012. spin_lock_init(&ha->mbx_reg_lock);
  1013. ha->dpc_pid = -1;
  1014. init_completion(&ha->dpc_inited);
  1015. init_completion(&ha->dpc_exited);
  1016. qla2x00_config_dma_addressing(ha);
  1017. if (qla2x00_mem_alloc(ha)) {
  1018. qla_printk(KERN_WARNING, ha,
  1019. "[ERROR] Failed to allocate memory for adapter\n");
  1020. goto probe_alloc_failed;
  1021. }
  1022. pci_set_drvdata(pdev, ha);
  1023. host->this_id = 255;
  1024. host->cmd_per_lun = 3;
  1025. host->unique_id = ha->instance;
  1026. host->max_cmd_len = MAX_CMDSZ;
  1027. host->max_channel = ha->ports - 1;
  1028. host->max_lun = MAX_LUNS;
  1029. host->transportt = qla2xxx_transport_template;
  1030. if (scsi_add_host(host, &pdev->dev))
  1031. goto probe_alloc_failed;
  1032. qla2x00_alloc_sysfs_attr(ha);
  1033. if (qla2x00_initialize_adapter(ha) &&
  1034. !(ha->device_flags & DFLG_NO_CABLE)) {
  1035. qla_printk(KERN_WARNING, ha,
  1036. "Failed to initialize adapter\n");
  1037. DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
  1038. "Adapter flags %x.\n",
  1039. ha->host_no, ha->device_flags));
  1040. goto probe_failed;
  1041. }
  1042. qla2x00_init_host_attr(ha);
  1043. /*
  1044. * Startup the kernel thread for this host adapter
  1045. */
  1046. ha->dpc_should_die = 0;
  1047. ha->dpc_pid = kernel_thread(qla2x00_do_dpc, ha, 0);
  1048. if (ha->dpc_pid < 0) {
  1049. qla_printk(KERN_WARNING, ha,
  1050. "Unable to start DPC thread!\n");
  1051. goto probe_failed;
  1052. }
  1053. wait_for_completion(&ha->dpc_inited);
  1054. if (IS_QLA2100(ha) || IS_QLA2200(ha))
  1055. ret = request_irq(host->irq, qla2100_intr_handler,
  1056. SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
  1057. else
  1058. ret = request_irq(host->irq, qla2300_intr_handler,
  1059. SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
  1060. if (ret != 0) {
  1061. qla_printk(KERN_WARNING, ha,
  1062. "Failed to reserve interrupt %d already in use.\n",
  1063. host->irq);
  1064. goto probe_failed;
  1065. }
  1066. /* Initialized the timer */
  1067. qla2x00_start_timer(ha, qla2x00_timer, WATCH_INTERVAL);
  1068. DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
  1069. ha->host_no, ha));
  1070. reg = ha->iobase;
  1071. /* Disable ISP interrupts. */
  1072. qla2x00_disable_intrs(ha);
  1073. /* Ensure mailbox registers are free. */
  1074. spin_lock_irqsave(&ha->hardware_lock, flags);
  1075. WRT_REG_WORD(&reg->semaphore, 0);
  1076. WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
  1077. WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
  1078. /* Enable proper parity */
  1079. if (!IS_QLA2100(ha) && !IS_QLA2200(ha)) {
  1080. if (IS_QLA2300(ha))
  1081. /* SRAM parity */
  1082. WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x1));
  1083. else
  1084. /* SRAM, Instruction RAM and GP RAM parity */
  1085. WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x7));
  1086. }
  1087. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1088. /* Enable chip interrupts. */
  1089. qla2x00_enable_intrs(ha);
  1090. /* v2.19.5b6 */
  1091. /*
  1092. * Wait around max loop_reset_delay secs for the devices to come
  1093. * on-line. We don't want Linux scanning before we are ready.
  1094. *
  1095. */
  1096. for (wait_switch = jiffies + (ha->loop_reset_delay * HZ);
  1097. time_before(jiffies,wait_switch) &&
  1098. !(ha->device_flags & (DFLG_NO_CABLE | DFLG_FABRIC_DEVICES))
  1099. && (ha->device_flags & SWITCH_FOUND) ;) {
  1100. qla2x00_check_fabric_devices(ha);
  1101. msleep(10);
  1102. }
  1103. ha->flags.init_done = 1;
  1104. num_hosts++;
  1105. qla_printk(KERN_INFO, ha, "\n"
  1106. " QLogic Fibre Channel HBA Driver: %s\n"
  1107. " QLogic %s - %s\n"
  1108. " %s: %s @ %s hdma%c, host#=%ld, fw=%s\n", qla2x00_version_str,
  1109. ha->model_number, ha->model_desc ? ha->model_desc: "",
  1110. ha->brd_info->isp_name, qla2x00_get_pci_info_str(ha, pci_info),
  1111. pci_name(ha->pdev), ha->flags.enable_64bit_addressing ? '+': '-',
  1112. ha->host_no, qla2x00_get_fw_version_str(ha, fw_str));
  1113. /* Go with fc_rport registration. */
  1114. list_for_each_entry(fcport, &ha->fcports, list)
  1115. qla2x00_reg_remote_port(ha, fcport);
  1116. return 0;
  1117. probe_failed:
  1118. fc_remove_host(ha->host);
  1119. scsi_remove_host(host);
  1120. probe_alloc_failed:
  1121. qla2x00_free_device(ha);
  1122. scsi_host_put(host);
  1123. probe_disable_device:
  1124. pci_disable_device(pdev);
  1125. return -1;
  1126. }
  1127. EXPORT_SYMBOL_GPL(qla2x00_probe_one);
  1128. void qla2x00_remove_one(struct pci_dev *pdev)
  1129. {
  1130. scsi_qla_host_t *ha;
  1131. ha = pci_get_drvdata(pdev);
  1132. qla2x00_free_sysfs_attr(ha);
  1133. fc_remove_host(ha->host);
  1134. scsi_remove_host(ha->host);
  1135. qla2x00_free_device(ha);
  1136. scsi_host_put(ha->host);
  1137. pci_set_drvdata(pdev, NULL);
  1138. }
  1139. EXPORT_SYMBOL_GPL(qla2x00_remove_one);
  1140. static void
  1141. qla2x00_free_device(scsi_qla_host_t *ha)
  1142. {
  1143. int ret;
  1144. /* Abort any outstanding IO descriptors. */
  1145. if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
  1146. qla2x00_cancel_io_descriptors(ha);
  1147. /* turn-off interrupts on the card */
  1148. if (ha->interrupts_on)
  1149. qla2x00_disable_intrs(ha);
  1150. /* Disable timer */
  1151. if (ha->timer_active)
  1152. qla2x00_stop_timer(ha);
  1153. /* Kill the kernel thread for this host */
  1154. if (ha->dpc_pid >= 0) {
  1155. ha->dpc_should_die = 1;
  1156. wmb();
  1157. ret = kill_proc(ha->dpc_pid, SIGHUP, 1);
  1158. if (ret) {
  1159. qla_printk(KERN_ERR, ha,
  1160. "Unable to signal DPC thread -- (%d)\n", ret);
  1161. /* TODO: SOMETHING MORE??? */
  1162. } else {
  1163. wait_for_completion(&ha->dpc_exited);
  1164. }
  1165. }
  1166. qla2x00_mem_free(ha);
  1167. ha->flags.online = 0;
  1168. /* Detach interrupts */
  1169. if (ha->pdev->irq)
  1170. free_irq(ha->pdev->irq, ha);
  1171. /* release io space registers */
  1172. if (ha->iobase)
  1173. iounmap(ha->iobase);
  1174. pci_release_regions(ha->pdev);
  1175. pci_disable_device(ha->pdev);
  1176. }
  1177. /*
  1178. * qla2x00_mark_device_lost Updates fcport state when device goes offline.
  1179. *
  1180. * Input: ha = adapter block pointer. fcport = port structure pointer.
  1181. *
  1182. * Return: None.
  1183. *
  1184. * Context:
  1185. */
  1186. void qla2x00_mark_device_lost(scsi_qla_host_t *ha, fc_port_t *fcport,
  1187. int do_login)
  1188. {
  1189. if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
  1190. fc_remote_port_block(fcport->rport);
  1191. /*
  1192. * We may need to retry the login, so don't change the state of the
  1193. * port but do the retries.
  1194. */
  1195. if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
  1196. atomic_set(&fcport->state, FCS_DEVICE_LOST);
  1197. if (!do_login)
  1198. return;
  1199. if (fcport->login_retry == 0) {
  1200. fcport->login_retry = ha->login_retry_count;
  1201. set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
  1202. DEBUG(printk("scsi(%ld): Port login retry: "
  1203. "%02x%02x%02x%02x%02x%02x%02x%02x, "
  1204. "id = 0x%04x retry cnt=%d\n",
  1205. ha->host_no,
  1206. fcport->port_name[0],
  1207. fcport->port_name[1],
  1208. fcport->port_name[2],
  1209. fcport->port_name[3],
  1210. fcport->port_name[4],
  1211. fcport->port_name[5],
  1212. fcport->port_name[6],
  1213. fcport->port_name[7],
  1214. fcport->loop_id,
  1215. fcport->login_retry));
  1216. }
  1217. }
  1218. /*
  1219. * qla2x00_mark_all_devices_lost
  1220. * Updates fcport state when device goes offline.
  1221. *
  1222. * Input:
  1223. * ha = adapter block pointer.
  1224. * fcport = port structure pointer.
  1225. *
  1226. * Return:
  1227. * None.
  1228. *
  1229. * Context:
  1230. */
  1231. void
  1232. qla2x00_mark_all_devices_lost(scsi_qla_host_t *ha)
  1233. {
  1234. fc_port_t *fcport;
  1235. list_for_each_entry(fcport, &ha->fcports, list) {
  1236. if (fcport->port_type != FCT_TARGET)
  1237. continue;
  1238. /*
  1239. * No point in marking the device as lost, if the device is
  1240. * already DEAD.
  1241. */
  1242. if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
  1243. continue;
  1244. if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
  1245. fc_remote_port_block(fcport->rport);
  1246. atomic_set(&fcport->state, FCS_DEVICE_LOST);
  1247. }
  1248. }
  1249. /*
  1250. * qla2x00_mem_alloc
  1251. * Allocates adapter memory.
  1252. *
  1253. * Returns:
  1254. * 0 = success.
  1255. * 1 = failure.
  1256. */
  1257. static uint8_t
  1258. qla2x00_mem_alloc(scsi_qla_host_t *ha)
  1259. {
  1260. char name[16];
  1261. uint8_t status = 1;
  1262. int retry= 10;
  1263. do {
  1264. /*
  1265. * This will loop only once if everything goes well, else some
  1266. * number of retries will be performed to get around a kernel
  1267. * bug where available mem is not allocated until after a
  1268. * little delay and a retry.
  1269. */
  1270. ha->request_ring = dma_alloc_coherent(&ha->pdev->dev,
  1271. (ha->request_q_length + 1) * sizeof(request_t),
  1272. &ha->request_dma, GFP_KERNEL);
  1273. if (ha->request_ring == NULL) {
  1274. qla_printk(KERN_WARNING, ha,
  1275. "Memory Allocation failed - request_ring\n");
  1276. qla2x00_mem_free(ha);
  1277. msleep(100);
  1278. continue;
  1279. }
  1280. ha->response_ring = dma_alloc_coherent(&ha->pdev->dev,
  1281. (ha->response_q_length + 1) * sizeof(response_t),
  1282. &ha->response_dma, GFP_KERNEL);
  1283. if (ha->response_ring == NULL) {
  1284. qla_printk(KERN_WARNING, ha,
  1285. "Memory Allocation failed - response_ring\n");
  1286. qla2x00_mem_free(ha);
  1287. msleep(100);
  1288. continue;
  1289. }
  1290. ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
  1291. &ha->gid_list_dma, GFP_KERNEL);
  1292. if (ha->gid_list == NULL) {
  1293. qla_printk(KERN_WARNING, ha,
  1294. "Memory Allocation failed - gid_list\n");
  1295. qla2x00_mem_free(ha);
  1296. msleep(100);
  1297. continue;
  1298. }
  1299. ha->rlc_rsp = dma_alloc_coherent(&ha->pdev->dev,
  1300. sizeof(rpt_lun_cmd_rsp_t), &ha->rlc_rsp_dma, GFP_KERNEL);
  1301. if (ha->rlc_rsp == NULL) {
  1302. qla_printk(KERN_WARNING, ha,
  1303. "Memory Allocation failed - rlc");
  1304. qla2x00_mem_free(ha);
  1305. msleep(100);
  1306. continue;
  1307. }
  1308. snprintf(name, sizeof(name), "qla2xxx_%ld", ha->host_no);
  1309. ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
  1310. DMA_POOL_SIZE, 8, 0);
  1311. if (ha->s_dma_pool == NULL) {
  1312. qla_printk(KERN_WARNING, ha,
  1313. "Memory Allocation failed - s_dma_pool\n");
  1314. qla2x00_mem_free(ha);
  1315. msleep(100);
  1316. continue;
  1317. }
  1318. /* get consistent memory allocated for init control block */
  1319. ha->init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
  1320. &ha->init_cb_dma);
  1321. if (ha->init_cb == NULL) {
  1322. qla_printk(KERN_WARNING, ha,
  1323. "Memory Allocation failed - init_cb\n");
  1324. qla2x00_mem_free(ha);
  1325. msleep(100);
  1326. continue;
  1327. }
  1328. memset(ha->init_cb, 0, sizeof(init_cb_t));
  1329. /* Get consistent memory allocated for Get Port Database cmd */
  1330. ha->iodesc_pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
  1331. &ha->iodesc_pd_dma);
  1332. if (ha->iodesc_pd == NULL) {
  1333. /* error */
  1334. qla_printk(KERN_WARNING, ha,
  1335. "Memory Allocation failed - iodesc_pd\n");
  1336. qla2x00_mem_free(ha);
  1337. msleep(100);
  1338. continue;
  1339. }
  1340. memset(ha->iodesc_pd, 0, PORT_DATABASE_SIZE);
  1341. /* Allocate ioctl related memory. */
  1342. if (qla2x00_alloc_ioctl_mem(ha)) {
  1343. qla_printk(KERN_WARNING, ha,
  1344. "Memory Allocation failed - ioctl_mem\n");
  1345. qla2x00_mem_free(ha);
  1346. msleep(100);
  1347. continue;
  1348. }
  1349. if (qla2x00_allocate_sp_pool(ha)) {
  1350. qla_printk(KERN_WARNING, ha,
  1351. "Memory Allocation failed - "
  1352. "qla2x00_allocate_sp_pool()\n");
  1353. qla2x00_mem_free(ha);
  1354. msleep(100);
  1355. continue;
  1356. }
  1357. /* Allocate memory for SNS commands */
  1358. if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
  1359. /* Get consistent memory allocated for SNS commands */
  1360. ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
  1361. sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma,
  1362. GFP_KERNEL);
  1363. if (ha->sns_cmd == NULL) {
  1364. /* error */
  1365. qla_printk(KERN_WARNING, ha,
  1366. "Memory Allocation failed - sns_cmd\n");
  1367. qla2x00_mem_free(ha);
  1368. msleep(100);
  1369. continue;
  1370. }
  1371. memset(ha->sns_cmd, 0, sizeof(struct sns_cmd_pkt));
  1372. } else {
  1373. /* Get consistent memory allocated for MS IOCB */
  1374. ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
  1375. &ha->ms_iocb_dma);
  1376. if (ha->ms_iocb == NULL) {
  1377. /* error */
  1378. qla_printk(KERN_WARNING, ha,
  1379. "Memory Allocation failed - ms_iocb\n");
  1380. qla2x00_mem_free(ha);
  1381. msleep(100);
  1382. continue;
  1383. }
  1384. memset(ha->ms_iocb, 0, sizeof(ms_iocb_entry_t));
  1385. /*
  1386. * Get consistent memory allocated for CT SNS
  1387. * commands
  1388. */
  1389. ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
  1390. sizeof(struct ct_sns_pkt), &ha->ct_sns_dma,
  1391. GFP_KERNEL);
  1392. if (ha->ct_sns == NULL) {
  1393. /* error */
  1394. qla_printk(KERN_WARNING, ha,
  1395. "Memory Allocation failed - ct_sns\n");
  1396. qla2x00_mem_free(ha);
  1397. msleep(100);
  1398. continue;
  1399. }
  1400. memset(ha->ct_sns, 0, sizeof(struct ct_sns_pkt));
  1401. }
  1402. /* Done all allocations without any error. */
  1403. status = 0;
  1404. } while (retry-- && status != 0);
  1405. if (status) {
  1406. printk(KERN_WARNING
  1407. "%s(): **** FAILED ****\n", __func__);
  1408. }
  1409. return(status);
  1410. }
  1411. /*
  1412. * qla2x00_mem_free
  1413. * Frees all adapter allocated memory.
  1414. *
  1415. * Input:
  1416. * ha = adapter block pointer.
  1417. */
  1418. static void
  1419. qla2x00_mem_free(scsi_qla_host_t *ha)
  1420. {
  1421. struct list_head *fcpl, *fcptemp;
  1422. fc_port_t *fcport;
  1423. unsigned long wtime;/* max wait time if mbx cmd is busy. */
  1424. if (ha == NULL) {
  1425. /* error */
  1426. DEBUG2(printk("%s(): ERROR invalid ha pointer.\n", __func__));
  1427. return;
  1428. }
  1429. /* Make sure all other threads are stopped. */
  1430. wtime = 60 * HZ;
  1431. while (ha->dpc_wait && wtime) {
  1432. set_current_state(TASK_INTERRUPTIBLE);
  1433. wtime = schedule_timeout(wtime);
  1434. }
  1435. /* free ioctl memory */
  1436. qla2x00_free_ioctl_mem(ha);
  1437. /* free sp pool */
  1438. qla2x00_free_sp_pool(ha);
  1439. if (ha->sns_cmd)
  1440. dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
  1441. ha->sns_cmd, ha->sns_cmd_dma);
  1442. if (ha->ct_sns)
  1443. dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
  1444. ha->ct_sns, ha->ct_sns_dma);
  1445. if (ha->ms_iocb)
  1446. dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
  1447. if (ha->iodesc_pd)
  1448. dma_pool_free(ha->s_dma_pool, ha->iodesc_pd, ha->iodesc_pd_dma);
  1449. if (ha->init_cb)
  1450. dma_pool_free(ha->s_dma_pool, ha->init_cb, ha->init_cb_dma);
  1451. if (ha->s_dma_pool)
  1452. dma_pool_destroy(ha->s_dma_pool);
  1453. if (ha->rlc_rsp)
  1454. dma_free_coherent(&ha->pdev->dev,
  1455. sizeof(rpt_lun_cmd_rsp_t), ha->rlc_rsp,
  1456. ha->rlc_rsp_dma);
  1457. if (ha->gid_list)
  1458. dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
  1459. ha->gid_list_dma);
  1460. if (ha->response_ring)
  1461. dma_free_coherent(&ha->pdev->dev,
  1462. (ha->response_q_length + 1) * sizeof(response_t),
  1463. ha->response_ring, ha->response_dma);
  1464. if (ha->request_ring)
  1465. dma_free_coherent(&ha->pdev->dev,
  1466. (ha->request_q_length + 1) * sizeof(request_t),
  1467. ha->request_ring, ha->request_dma);
  1468. ha->sns_cmd = NULL;
  1469. ha->sns_cmd_dma = 0;
  1470. ha->ct_sns = NULL;
  1471. ha->ct_sns_dma = 0;
  1472. ha->ms_iocb = NULL;
  1473. ha->ms_iocb_dma = 0;
  1474. ha->iodesc_pd = NULL;
  1475. ha->iodesc_pd_dma = 0;
  1476. ha->init_cb = NULL;
  1477. ha->init_cb_dma = 0;
  1478. ha->s_dma_pool = NULL;
  1479. ha->rlc_rsp = NULL;
  1480. ha->rlc_rsp_dma = 0;
  1481. ha->gid_list = NULL;
  1482. ha->gid_list_dma = 0;
  1483. ha->response_ring = NULL;
  1484. ha->response_dma = 0;
  1485. ha->request_ring = NULL;
  1486. ha->request_dma = 0;
  1487. list_for_each_safe(fcpl, fcptemp, &ha->fcports) {
  1488. fcport = list_entry(fcpl, fc_port_t, list);
  1489. /* fc ports */
  1490. list_del_init(&fcport->list);
  1491. kfree(fcport);
  1492. }
  1493. INIT_LIST_HEAD(&ha->fcports);
  1494. if (ha->fw_dump)
  1495. free_pages((unsigned long)ha->fw_dump, ha->fw_dump_order);
  1496. if (ha->fw_dump_buffer)
  1497. vfree(ha->fw_dump_buffer);
  1498. ha->fw_dump = NULL;
  1499. ha->fw_dump_reading = 0;
  1500. ha->fw_dump_buffer = NULL;
  1501. }
  1502. /*
  1503. * qla2x00_allocate_sp_pool
  1504. * This routine is called during initialization to allocate
  1505. * memory for local srb_t.
  1506. *
  1507. * Input:
  1508. * ha = adapter block pointer.
  1509. *
  1510. * Context:
  1511. * Kernel context.
  1512. *
  1513. * Note: Sets the ref_count for non Null sp to one.
  1514. */
  1515. static int
  1516. qla2x00_allocate_sp_pool(scsi_qla_host_t *ha)
  1517. {
  1518. int rval;
  1519. rval = QLA_SUCCESS;
  1520. ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
  1521. mempool_free_slab, srb_cachep);
  1522. if (ha->srb_mempool == NULL) {
  1523. qla_printk(KERN_INFO, ha, "Unable to allocate SRB mempool.\n");
  1524. rval = QLA_FUNCTION_FAILED;
  1525. }
  1526. return (rval);
  1527. }
  1528. /*
  1529. * This routine frees all adapter allocated memory.
  1530. *
  1531. */
  1532. static void
  1533. qla2x00_free_sp_pool( scsi_qla_host_t *ha)
  1534. {
  1535. if (ha->srb_mempool) {
  1536. mempool_destroy(ha->srb_mempool);
  1537. ha->srb_mempool = NULL;
  1538. }
  1539. }
  1540. /**************************************************************************
  1541. * qla2x00_do_dpc
  1542. * This kernel thread is a task that is schedule by the interrupt handler
  1543. * to perform the background processing for interrupts.
  1544. *
  1545. * Notes:
  1546. * This task always run in the context of a kernel thread. It
  1547. * is kick-off by the driver's detect code and starts up
  1548. * up one per adapter. It immediately goes to sleep and waits for
  1549. * some fibre event. When either the interrupt handler or
  1550. * the timer routine detects a event it will one of the task
  1551. * bits then wake us up.
  1552. **************************************************************************/
  1553. static int
  1554. qla2x00_do_dpc(void *data)
  1555. {
  1556. DECLARE_MUTEX_LOCKED(sem);
  1557. scsi_qla_host_t *ha;
  1558. fc_port_t *fcport;
  1559. uint8_t status;
  1560. uint16_t next_loopid;
  1561. ha = (scsi_qla_host_t *)data;
  1562. lock_kernel();
  1563. daemonize("%s_dpc", ha->host_str);
  1564. allow_signal(SIGHUP);
  1565. ha->dpc_wait = &sem;
  1566. set_user_nice(current, -20);
  1567. unlock_kernel();
  1568. complete(&ha->dpc_inited);
  1569. while (1) {
  1570. DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
  1571. if (down_interruptible(&sem))
  1572. break;
  1573. if (ha->dpc_should_die)
  1574. break;
  1575. DEBUG3(printk("qla2x00: DPC handler waking up\n"));
  1576. /* Initialization not yet finished. Don't do anything yet. */
  1577. if (!ha->flags.init_done || ha->dpc_active)
  1578. continue;
  1579. DEBUG3(printk("scsi(%ld): DPC handler\n", ha->host_no));
  1580. ha->dpc_active = 1;
  1581. if (ha->flags.mbox_busy) {
  1582. ha->dpc_active = 0;
  1583. continue;
  1584. }
  1585. if (test_and_clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
  1586. DEBUG(printk("scsi(%ld): dpc: sched "
  1587. "qla2x00_abort_isp ha = %p\n",
  1588. ha->host_no, ha));
  1589. if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
  1590. &ha->dpc_flags))) {
  1591. if (qla2x00_abort_isp(ha)) {
  1592. /* failed. retry later */
  1593. set_bit(ISP_ABORT_NEEDED,
  1594. &ha->dpc_flags);
  1595. }
  1596. clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  1597. }
  1598. DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
  1599. ha->host_no));
  1600. }
  1601. if (test_and_clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) &&
  1602. (!(test_and_set_bit(RESET_ACTIVE, &ha->dpc_flags)))) {
  1603. DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
  1604. ha->host_no));
  1605. qla2x00_rst_aen(ha);
  1606. clear_bit(RESET_ACTIVE, &ha->dpc_flags);
  1607. }
  1608. /* Retry each device up to login retry count */
  1609. if ((test_and_clear_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
  1610. !test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) &&
  1611. atomic_read(&ha->loop_state) != LOOP_DOWN) {
  1612. DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
  1613. ha->host_no));
  1614. next_loopid = 0;
  1615. list_for_each_entry(fcport, &ha->fcports, list) {
  1616. if (fcport->port_type != FCT_TARGET)
  1617. continue;
  1618. /*
  1619. * If the port is not ONLINE then try to login
  1620. * to it if we haven't run out of retries.
  1621. */
  1622. if (atomic_read(&fcport->state) != FCS_ONLINE &&
  1623. fcport->login_retry) {
  1624. fcport->login_retry--;
  1625. if (fcport->flags & FCF_FABRIC_DEVICE) {
  1626. if (fcport->flags &
  1627. FCF_TAPE_PRESENT)
  1628. qla2x00_fabric_logout(
  1629. ha,
  1630. fcport->loop_id);
  1631. status = qla2x00_fabric_login(
  1632. ha, fcport, &next_loopid);
  1633. } else
  1634. status =
  1635. qla2x00_local_device_login(
  1636. ha, fcport->loop_id);
  1637. if (status == QLA_SUCCESS) {
  1638. fcport->old_loop_id = fcport->loop_id;
  1639. DEBUG(printk("scsi(%ld): port login OK: logged in ID 0x%x\n",
  1640. ha->host_no, fcport->loop_id));
  1641. fcport->port_login_retry_count =
  1642. ha->port_down_retry_count * PORT_RETRY_TIME;
  1643. atomic_set(&fcport->state, FCS_ONLINE);
  1644. atomic_set(&fcport->port_down_timer,
  1645. ha->port_down_retry_count * PORT_RETRY_TIME);
  1646. fcport->login_retry = 0;
  1647. } else if (status == 1) {
  1648. set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
  1649. /* retry the login again */
  1650. DEBUG(printk("scsi(%ld): Retrying %d login again loop_id 0x%x\n",
  1651. ha->host_no,
  1652. fcport->login_retry, fcport->loop_id));
  1653. } else {
  1654. fcport->login_retry = 0;
  1655. }
  1656. }
  1657. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  1658. break;
  1659. }
  1660. DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
  1661. ha->host_no));
  1662. }
  1663. if ((test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags)) &&
  1664. atomic_read(&ha->loop_state) != LOOP_DOWN) {
  1665. clear_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags);
  1666. DEBUG(printk("scsi(%ld): qla2x00_login_retry()\n",
  1667. ha->host_no));
  1668. set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  1669. DEBUG(printk("scsi(%ld): qla2x00_login_retry - end\n",
  1670. ha->host_no));
  1671. }
  1672. if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1673. DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
  1674. ha->host_no));
  1675. if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
  1676. &ha->dpc_flags))) {
  1677. qla2x00_loop_resync(ha);
  1678. clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
  1679. }
  1680. DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
  1681. ha->host_no));
  1682. }
  1683. if (test_and_clear_bit(FCPORT_RESCAN_NEEDED, &ha->dpc_flags)) {
  1684. DEBUG(printk("scsi(%ld): Rescan flagged fcports...\n",
  1685. ha->host_no));
  1686. qla2x00_rescan_fcports(ha);
  1687. DEBUG(printk("scsi(%ld): Rescan flagged fcports..."
  1688. "end.\n",
  1689. ha->host_no));
  1690. }
  1691. if (!ha->interrupts_on)
  1692. qla2x00_enable_intrs(ha);
  1693. ha->dpc_active = 0;
  1694. } /* End of while(1) */
  1695. DEBUG(printk("scsi(%ld): DPC handler exiting\n", ha->host_no));
  1696. /*
  1697. * Make sure that nobody tries to wake us up again.
  1698. */
  1699. ha->dpc_wait = NULL;
  1700. ha->dpc_active = 0;
  1701. complete_and_exit(&ha->dpc_exited, 0);
  1702. }
  1703. /*
  1704. * qla2x00_rst_aen
  1705. * Processes asynchronous reset.
  1706. *
  1707. * Input:
  1708. * ha = adapter block pointer.
  1709. */
  1710. static void
  1711. qla2x00_rst_aen(scsi_qla_host_t *ha)
  1712. {
  1713. if (ha->flags.online && !ha->flags.reset_active &&
  1714. !atomic_read(&ha->loop_down_timer) &&
  1715. !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags))) {
  1716. do {
  1717. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  1718. /*
  1719. * Issue marker command only when we are going to start
  1720. * the I/O.
  1721. */
  1722. ha->marker_needed = 1;
  1723. } while (!atomic_read(&ha->loop_down_timer) &&
  1724. (test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags)));
  1725. }
  1726. }
  1727. /*
  1728. * This routine will allocate SP from the free queue
  1729. * input:
  1730. * scsi_qla_host_t *
  1731. * output:
  1732. * srb_t * or NULL
  1733. */
  1734. static srb_t *
  1735. qla2x00_get_new_sp(scsi_qla_host_t *ha)
  1736. {
  1737. srb_t *sp;
  1738. sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
  1739. if (sp)
  1740. atomic_set(&sp->ref_count, 1);
  1741. return (sp);
  1742. }
  1743. static void
  1744. qla2x00_sp_free_dma(scsi_qla_host_t *ha, srb_t *sp)
  1745. {
  1746. struct scsi_cmnd *cmd = sp->cmd;
  1747. if (sp->flags & SRB_DMA_VALID) {
  1748. if (cmd->use_sg) {
  1749. dma_unmap_sg(&ha->pdev->dev, cmd->request_buffer,
  1750. cmd->use_sg, cmd->sc_data_direction);
  1751. } else if (cmd->request_bufflen) {
  1752. dma_unmap_single(&ha->pdev->dev, sp->dma_handle,
  1753. cmd->request_bufflen, cmd->sc_data_direction);
  1754. }
  1755. sp->flags &= ~SRB_DMA_VALID;
  1756. }
  1757. }
  1758. void
  1759. qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *sp)
  1760. {
  1761. struct scsi_cmnd *cmd = sp->cmd;
  1762. qla2x00_sp_free_dma(ha, sp);
  1763. CMD_SP(cmd) = NULL;
  1764. mempool_free(sp, ha->srb_mempool);
  1765. cmd->scsi_done(cmd);
  1766. }
  1767. /**************************************************************************
  1768. * qla2x00_timer
  1769. *
  1770. * Description:
  1771. * One second timer
  1772. *
  1773. * Context: Interrupt
  1774. ***************************************************************************/
  1775. static void
  1776. qla2x00_timer(scsi_qla_host_t *ha)
  1777. {
  1778. unsigned long cpu_flags = 0;
  1779. fc_port_t *fcport;
  1780. int start_dpc = 0;
  1781. int index;
  1782. srb_t *sp;
  1783. int t;
  1784. /*
  1785. * Ports - Port down timer.
  1786. *
  1787. * Whenever, a port is in the LOST state we start decrementing its port
  1788. * down timer every second until it reaches zero. Once it reaches zero
  1789. * the port it marked DEAD.
  1790. */
  1791. t = 0;
  1792. list_for_each_entry(fcport, &ha->fcports, list) {
  1793. if (fcport->port_type != FCT_TARGET)
  1794. continue;
  1795. if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
  1796. if (atomic_read(&fcport->port_down_timer) == 0)
  1797. continue;
  1798. if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
  1799. atomic_set(&fcport->state, FCS_DEVICE_DEAD);
  1800. DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
  1801. "%d remainning\n",
  1802. ha->host_no,
  1803. t, atomic_read(&fcport->port_down_timer)));
  1804. }
  1805. t++;
  1806. } /* End of for fcport */
  1807. /* Loop down handler. */
  1808. if (atomic_read(&ha->loop_down_timer) > 0 &&
  1809. !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) && ha->flags.online) {
  1810. if (atomic_read(&ha->loop_down_timer) ==
  1811. ha->loop_down_abort_time) {
  1812. DEBUG(printk("scsi(%ld): Loop Down - aborting the "
  1813. "queues before time expire\n",
  1814. ha->host_no));
  1815. if (!IS_QLA2100(ha) && ha->link_down_timeout)
  1816. atomic_set(&ha->loop_state, LOOP_DEAD);
  1817. /* Schedule an ISP abort to return any tape commands. */
  1818. spin_lock_irqsave(&ha->hardware_lock, cpu_flags);
  1819. for (index = 1; index < MAX_OUTSTANDING_COMMANDS;
  1820. index++) {
  1821. fc_port_t *sfcp;
  1822. sp = ha->outstanding_cmds[index];
  1823. if (!sp)
  1824. continue;
  1825. sfcp = sp->fcport;
  1826. if (!(sfcp->flags & FCF_TAPE_PRESENT))
  1827. continue;
  1828. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1829. break;
  1830. }
  1831. spin_unlock_irqrestore(&ha->hardware_lock, cpu_flags);
  1832. set_bit(ABORT_QUEUES_NEEDED, &ha->dpc_flags);
  1833. start_dpc++;
  1834. }
  1835. /* if the loop has been down for 4 minutes, reinit adapter */
  1836. if (atomic_dec_and_test(&ha->loop_down_timer) != 0) {
  1837. DEBUG(printk("scsi(%ld): Loop down exceed 4 mins - "
  1838. "restarting queues.\n",
  1839. ha->host_no));
  1840. set_bit(RESTART_QUEUES_NEEDED, &ha->dpc_flags);
  1841. start_dpc++;
  1842. if (!(ha->device_flags & DFLG_NO_CABLE)) {
  1843. DEBUG(printk("scsi(%ld): Loop down - "
  1844. "aborting ISP.\n",
  1845. ha->host_no));
  1846. qla_printk(KERN_WARNING, ha,
  1847. "Loop down - aborting ISP.\n");
  1848. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1849. }
  1850. }
  1851. DEBUG3(printk("scsi(%ld): Loop Down - seconds remainning %d\n",
  1852. ha->host_no,
  1853. atomic_read(&ha->loop_down_timer)));
  1854. }
  1855. /* Schedule the DPC routine if needed */
  1856. if ((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) ||
  1857. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) ||
  1858. start_dpc ||
  1859. test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags) ||
  1860. test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) ||
  1861. test_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
  1862. ha->dpc_wait && !ha->dpc_active) {
  1863. up(ha->dpc_wait);
  1864. }
  1865. qla2x00_restart_timer(ha, WATCH_INTERVAL);
  1866. }
  1867. /* XXX(hch): crude hack to emulate a down_timeout() */
  1868. int
  1869. qla2x00_down_timeout(struct semaphore *sema, unsigned long timeout)
  1870. {
  1871. const unsigned int step = HZ/10;
  1872. do {
  1873. if (!down_trylock(sema))
  1874. return 0;
  1875. set_current_state(TASK_INTERRUPTIBLE);
  1876. if (schedule_timeout(step))
  1877. break;
  1878. } while ((timeout -= step) > 0);
  1879. return -ETIMEDOUT;
  1880. }
  1881. /**
  1882. * qla2x00_module_init - Module initialization.
  1883. **/
  1884. static int __init
  1885. qla2x00_module_init(void)
  1886. {
  1887. /* Allocate cache for SRBs. */
  1888. srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
  1889. SLAB_HWCACHE_ALIGN, NULL, NULL);
  1890. if (srb_cachep == NULL) {
  1891. printk(KERN_ERR
  1892. "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
  1893. return -ENOMEM;
  1894. }
  1895. /* Derive version string. */
  1896. strcpy(qla2x00_version_str, QLA2XXX_VERSION);
  1897. #if DEBUG_QLA2100
  1898. strcat(qla2x00_version_str, "-debug");
  1899. #endif
  1900. qla2xxx_transport_template =
  1901. fc_attach_transport(&qla2xxx_transport_functions);
  1902. if (!qla2xxx_transport_template)
  1903. return -ENODEV;
  1904. printk(KERN_INFO "QLogic Fibre Channel HBA Driver\n");
  1905. return 0;
  1906. }
  1907. /**
  1908. * qla2x00_module_exit - Module cleanup.
  1909. **/
  1910. static void __exit
  1911. qla2x00_module_exit(void)
  1912. {
  1913. kmem_cache_destroy(srb_cachep);
  1914. fc_release_transport(qla2xxx_transport_template);
  1915. }
  1916. module_init(qla2x00_module_init);
  1917. module_exit(qla2x00_module_exit);
  1918. MODULE_AUTHOR("QLogic Corporation");
  1919. MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
  1920. MODULE_LICENSE("GPL");
  1921. MODULE_VERSION(QLA2XXX_VERSION);