zfcp_erp.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686
  1. /*
  2. * zfcp device driver
  3. *
  4. * Error Recovery Procedures (ERP).
  5. *
  6. * Copyright IBM Corporation 2002, 2008
  7. */
  8. #include "zfcp_ext.h"
  9. static int zfcp_erp_adapter_reopen_internal(struct zfcp_adapter *, int, u8,
  10. void *);
  11. static int zfcp_erp_port_forced_reopen_internal(struct zfcp_port *, int, u8,
  12. void *);
  13. static int zfcp_erp_port_reopen_internal(struct zfcp_port *, int, u8, void *);
  14. static int zfcp_erp_unit_reopen_internal(struct zfcp_unit *, int, u8, void *);
  15. static int zfcp_erp_port_reopen_all_internal(struct zfcp_adapter *, int, u8,
  16. void *);
  17. static int zfcp_erp_unit_reopen_all_internal(struct zfcp_port *, int, u8,
  18. void *);
  19. static void zfcp_erp_adapter_block(struct zfcp_adapter *, int);
  20. static void zfcp_erp_adapter_unblock(struct zfcp_adapter *);
  21. static void zfcp_erp_port_block(struct zfcp_port *, int);
  22. static void zfcp_erp_port_unblock(struct zfcp_port *);
  23. static void zfcp_erp_unit_block(struct zfcp_unit *, int);
  24. static void zfcp_erp_unit_unblock(struct zfcp_unit *);
  25. static int zfcp_erp_thread(void *);
  26. static int zfcp_erp_strategy(struct zfcp_erp_action *);
  27. static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *);
  28. static int zfcp_erp_strategy_memwait(struct zfcp_erp_action *);
  29. static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *, int);
  30. static int zfcp_erp_strategy_check_unit(struct zfcp_unit *, int);
  31. static int zfcp_erp_strategy_check_port(struct zfcp_port *, int);
  32. static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *, int);
  33. static int zfcp_erp_strategy_statechange(int, u32, struct zfcp_adapter *,
  34. struct zfcp_port *,
  35. struct zfcp_unit *, int);
  36. static int zfcp_erp_strategy_statechange_detected(atomic_t *, u32);
  37. static int zfcp_erp_strategy_followup_actions(int, struct zfcp_adapter *,
  38. struct zfcp_port *,
  39. struct zfcp_unit *, int);
  40. static int zfcp_erp_strategy_check_queues(struct zfcp_adapter *);
  41. static int zfcp_erp_strategy_check_action(struct zfcp_erp_action *, int);
  42. static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *);
  43. static int zfcp_erp_adapter_strategy_generic(struct zfcp_erp_action *, int);
  44. static int zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *);
  45. static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *);
  46. static int zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *);
  47. static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *);
  48. static int zfcp_erp_adapter_strategy_open_fsf_xconfig(struct zfcp_erp_action *);
  49. static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *);
  50. static int zfcp_erp_adapter_strategy_open_fsf_statusread(
  51. struct zfcp_erp_action *);
  52. static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *);
  53. static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *);
  54. static int zfcp_erp_port_strategy(struct zfcp_erp_action *);
  55. static int zfcp_erp_port_strategy_clearstati(struct zfcp_port *);
  56. static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *);
  57. static int zfcp_erp_port_strategy_open(struct zfcp_erp_action *);
  58. static int zfcp_erp_port_strategy_open_nameserver(struct zfcp_erp_action *);
  59. static int zfcp_erp_port_strategy_open_nameserver_wakeup(
  60. struct zfcp_erp_action *);
  61. static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *);
  62. static int zfcp_erp_port_strategy_open_common_lookup(struct zfcp_erp_action *);
  63. static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *);
  64. static int zfcp_erp_unit_strategy(struct zfcp_erp_action *);
  65. static int zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *);
  66. static int zfcp_erp_unit_strategy_close(struct zfcp_erp_action *);
  67. static int zfcp_erp_unit_strategy_open(struct zfcp_erp_action *);
  68. static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *);
  69. static void zfcp_erp_action_dismiss_port(struct zfcp_port *);
  70. static void zfcp_erp_action_dismiss_unit(struct zfcp_unit *);
  71. static void zfcp_erp_action_dismiss(struct zfcp_erp_action *);
  72. static int zfcp_erp_action_enqueue(int, struct zfcp_adapter *,
  73. struct zfcp_port *, struct zfcp_unit *,
  74. u8 id, void *ref);
  75. static int zfcp_erp_action_dequeue(struct zfcp_erp_action *);
  76. static void zfcp_erp_action_cleanup(int, struct zfcp_adapter *,
  77. struct zfcp_port *, struct zfcp_unit *,
  78. int);
  79. static void zfcp_erp_action_ready(struct zfcp_erp_action *);
  80. static int zfcp_erp_action_exists(struct zfcp_erp_action *);
  81. static void zfcp_erp_action_to_ready(struct zfcp_erp_action *);
  82. static void zfcp_erp_action_to_running(struct zfcp_erp_action *);
  83. static void zfcp_erp_memwait_handler(unsigned long);
  84. /**
  85. * zfcp_close_fsf - stop FSF operations for an adapter
  86. *
  87. * Dismiss and cleanup all pending fsf_reqs (this wakes up all initiators of
  88. * requests waiting for completion; especially this returns SCSI commands
  89. * with error state).
  90. */
  91. static void zfcp_close_fsf(struct zfcp_adapter *adapter)
  92. {
  93. /* close queues to ensure that buffers are not accessed by adapter */
  94. zfcp_qdio_close(adapter);
  95. zfcp_fsf_req_dismiss_all(adapter);
  96. /* reset FSF request sequence number */
  97. adapter->fsf_req_seq_no = 0;
  98. /* all ports and units are closed */
  99. zfcp_erp_modify_adapter_status(adapter, 24, NULL,
  100. ZFCP_STATUS_COMMON_OPEN, ZFCP_CLEAR);
  101. }
  102. /**
  103. * zfcp_fsf_request_timeout_handler - called if a request timed out
  104. * @data: pointer to adapter for handler function
  105. *
  106. * This function needs to be called if requests (ELS, Generic Service,
  107. * or SCSI commands) exceed a certain time limit. The assumption is
  108. * that after the time limit the adapter get stuck. So we trigger a reopen of
  109. * the adapter.
  110. */
  111. static void zfcp_fsf_request_timeout_handler(unsigned long data)
  112. {
  113. struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
  114. zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED, 62,
  115. NULL);
  116. }
  117. void zfcp_fsf_start_timer(struct zfcp_fsf_req *fsf_req, unsigned long timeout)
  118. {
  119. fsf_req->timer.function = zfcp_fsf_request_timeout_handler;
  120. fsf_req->timer.data = (unsigned long) fsf_req->adapter;
  121. fsf_req->timer.expires = jiffies + timeout;
  122. add_timer(&fsf_req->timer);
  123. }
  124. /*
  125. * function:
  126. *
  127. * purpose: called if an adapter failed,
  128. * initiates adapter recovery which is done
  129. * asynchronously
  130. *
  131. * returns: 0 - initiated action successfully
  132. * <0 - failed to initiate action
  133. */
  134. static int zfcp_erp_adapter_reopen_internal(struct zfcp_adapter *adapter,
  135. int clear_mask, u8 id, void *ref)
  136. {
  137. int retval;
  138. zfcp_erp_adapter_block(adapter, clear_mask);
  139. if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
  140. /* ensure propagation of failed status to new devices */
  141. zfcp_erp_adapter_failed(adapter, 13, NULL);
  142. retval = -EIO;
  143. goto out;
  144. }
  145. retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
  146. adapter, NULL, NULL, id, ref);
  147. out:
  148. return retval;
  149. }
  150. /*
  151. * function:
  152. *
  153. * purpose: Wrappper for zfcp_erp_adapter_reopen_internal
  154. * used to ensure the correct locking
  155. *
  156. * returns: 0 - initiated action successfully
  157. * <0 - failed to initiate action
  158. */
  159. int zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear_mask,
  160. u8 id, void *ref)
  161. {
  162. int retval;
  163. unsigned long flags;
  164. read_lock_irqsave(&zfcp_data.config_lock, flags);
  165. write_lock(&adapter->erp_lock);
  166. retval = zfcp_erp_adapter_reopen_internal(adapter, clear_mask, id, ref);
  167. write_unlock(&adapter->erp_lock);
  168. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  169. return retval;
  170. }
  171. int zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear_mask,
  172. u8 id, void *ref)
  173. {
  174. int retval;
  175. retval = zfcp_erp_adapter_reopen(adapter,
  176. ZFCP_STATUS_COMMON_RUNNING |
  177. ZFCP_STATUS_COMMON_ERP_FAILED |
  178. clear_mask, id, ref);
  179. return retval;
  180. }
  181. int zfcp_erp_port_shutdown(struct zfcp_port *port, int clear_mask, u8 id,
  182. void *ref)
  183. {
  184. int retval;
  185. retval = zfcp_erp_port_reopen(port,
  186. ZFCP_STATUS_COMMON_RUNNING |
  187. ZFCP_STATUS_COMMON_ERP_FAILED |
  188. clear_mask, id, ref);
  189. return retval;
  190. }
  191. int zfcp_erp_unit_shutdown(struct zfcp_unit *unit, int clear_mask, u8 id,
  192. void *ref)
  193. {
  194. int retval;
  195. retval = zfcp_erp_unit_reopen(unit,
  196. ZFCP_STATUS_COMMON_RUNNING |
  197. ZFCP_STATUS_COMMON_ERP_FAILED |
  198. clear_mask, id, ref);
  199. return retval;
  200. }
  201. /*
  202. * function:
  203. *
  204. * purpose: called if a port failed to be opened normally
  205. * initiates Forced Reopen recovery which is done
  206. * asynchronously
  207. *
  208. * returns: 0 - initiated action successfully
  209. * <0 - failed to initiate action
  210. */
  211. static int zfcp_erp_port_forced_reopen_internal(struct zfcp_port *port,
  212. int clear_mask, u8 id,
  213. void *ref)
  214. {
  215. int retval;
  216. zfcp_erp_port_block(port, clear_mask);
  217. if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
  218. retval = -EIO;
  219. goto out;
  220. }
  221. retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
  222. port->adapter, port, NULL, id, ref);
  223. out:
  224. return retval;
  225. }
  226. /*
  227. * function:
  228. *
  229. * purpose: Wrappper for zfcp_erp_port_forced_reopen_internal
  230. * used to ensure the correct locking
  231. *
  232. * returns: 0 - initiated action successfully
  233. * <0 - failed to initiate action
  234. */
  235. int zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear_mask, u8 id,
  236. void *ref)
  237. {
  238. int retval;
  239. unsigned long flags;
  240. struct zfcp_adapter *adapter;
  241. adapter = port->adapter;
  242. read_lock_irqsave(&zfcp_data.config_lock, flags);
  243. write_lock(&adapter->erp_lock);
  244. retval = zfcp_erp_port_forced_reopen_internal(port, clear_mask, id,
  245. ref);
  246. write_unlock(&adapter->erp_lock);
  247. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  248. return retval;
  249. }
  250. /*
  251. * function:
  252. *
  253. * purpose: called if a port is to be opened
  254. * initiates Reopen recovery which is done
  255. * asynchronously
  256. *
  257. * returns: 0 - initiated action successfully
  258. * <0 - failed to initiate action
  259. */
  260. static int zfcp_erp_port_reopen_internal(struct zfcp_port *port, int clear_mask,
  261. u8 id, void *ref)
  262. {
  263. int retval;
  264. zfcp_erp_port_block(port, clear_mask);
  265. if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
  266. /* ensure propagation of failed status to new devices */
  267. zfcp_erp_port_failed(port, 14, NULL);
  268. retval = -EIO;
  269. goto out;
  270. }
  271. retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
  272. port->adapter, port, NULL, id, ref);
  273. out:
  274. return retval;
  275. }
  276. /**
  277. * zfcp_erp_port_reopen - initiate reopen of a remote port
  278. * @port: port to be reopened
  279. * @clear_mask: specifies flags in port status to be cleared
  280. * Return: 0 on success, < 0 on error
  281. *
  282. * This is a wrappper function for zfcp_erp_port_reopen_internal. It ensures
  283. * correct locking. An error recovery task is initiated to do the reopen.
  284. * To wait for the completion of the reopen zfcp_erp_wait should be used.
  285. */
  286. int zfcp_erp_port_reopen(struct zfcp_port *port, int clear_mask, u8 id,
  287. void *ref)
  288. {
  289. int retval;
  290. unsigned long flags;
  291. struct zfcp_adapter *adapter = port->adapter;
  292. read_lock_irqsave(&zfcp_data.config_lock, flags);
  293. write_lock(&adapter->erp_lock);
  294. retval = zfcp_erp_port_reopen_internal(port, clear_mask, id, ref);
  295. write_unlock(&adapter->erp_lock);
  296. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  297. return retval;
  298. }
  299. /*
  300. * function:
  301. *
  302. * purpose: called if a unit is to be opened
  303. * initiates Reopen recovery which is done
  304. * asynchronously
  305. *
  306. * returns: 0 - initiated action successfully
  307. * <0 - failed to initiate action
  308. */
  309. static int zfcp_erp_unit_reopen_internal(struct zfcp_unit *unit, int clear_mask,
  310. u8 id, void *ref)
  311. {
  312. int retval;
  313. struct zfcp_adapter *adapter = unit->port->adapter;
  314. zfcp_erp_unit_block(unit, clear_mask);
  315. if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status)) {
  316. retval = -EIO;
  317. goto out;
  318. }
  319. retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_UNIT,
  320. adapter, unit->port, unit, id, ref);
  321. out:
  322. return retval;
  323. }
  324. /**
  325. * zfcp_erp_unit_reopen - initiate reopen of a unit
  326. * @unit: unit to be reopened
  327. * @clear_mask: specifies flags in unit status to be cleared
  328. * Return: 0 on success, < 0 on error
  329. *
  330. * This is a wrappper for zfcp_erp_unit_reopen_internal. It ensures correct
  331. * locking. An error recovery task is initiated to do the reopen.
  332. * To wait for the completion of the reopen zfcp_erp_wait should be used.
  333. */
  334. int zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear_mask, u8 id,
  335. void *ref)
  336. {
  337. int retval;
  338. unsigned long flags;
  339. struct zfcp_adapter *adapter;
  340. struct zfcp_port *port;
  341. port = unit->port;
  342. adapter = port->adapter;
  343. read_lock_irqsave(&zfcp_data.config_lock, flags);
  344. write_lock(&adapter->erp_lock);
  345. retval = zfcp_erp_unit_reopen_internal(unit, clear_mask, id, ref);
  346. write_unlock(&adapter->erp_lock);
  347. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  348. return retval;
  349. }
  350. /**
  351. * zfcp_erp_adapter_block - mark adapter as blocked, block scsi requests
  352. */
  353. static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int clear_mask)
  354. {
  355. zfcp_erp_modify_adapter_status(adapter, 15, NULL,
  356. ZFCP_STATUS_COMMON_UNBLOCKED |
  357. clear_mask, ZFCP_CLEAR);
  358. }
  359. /* FIXME: isn't really atomic */
  360. /*
  361. * returns the mask which has not been set so far, i.e.
  362. * 0 if no bit has been changed, !0 if some bit has been changed
  363. */
  364. static int atomic_test_and_set_mask(unsigned long mask, atomic_t *v)
  365. {
  366. int changed_bits = (atomic_read(v) /*XOR*/^ mask) & mask;
  367. atomic_set_mask(mask, v);
  368. return changed_bits;
  369. }
  370. /* FIXME: isn't really atomic */
  371. /*
  372. * returns the mask which has not been cleared so far, i.e.
  373. * 0 if no bit has been changed, !0 if some bit has been changed
  374. */
  375. static int atomic_test_and_clear_mask(unsigned long mask, atomic_t *v)
  376. {
  377. int changed_bits = atomic_read(v) & mask;
  378. atomic_clear_mask(mask, v);
  379. return changed_bits;
  380. }
  381. /**
  382. * zfcp_erp_adapter_unblock - mark adapter as unblocked, allow scsi requests
  383. */
  384. static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
  385. {
  386. if (atomic_test_and_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
  387. &adapter->status))
  388. zfcp_rec_dbf_event_adapter(16, NULL, adapter);
  389. }
  390. /*
  391. * function:
  392. *
  393. * purpose: disable I/O,
  394. * return any open requests and clean them up,
  395. * aim: no pending and incoming I/O
  396. *
  397. * returns:
  398. */
  399. static void
  400. zfcp_erp_port_block(struct zfcp_port *port, int clear_mask)
  401. {
  402. zfcp_erp_modify_port_status(port, 17, NULL,
  403. ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
  404. ZFCP_CLEAR);
  405. }
  406. /*
  407. * function:
  408. *
  409. * purpose: enable I/O
  410. *
  411. * returns:
  412. */
  413. static void
  414. zfcp_erp_port_unblock(struct zfcp_port *port)
  415. {
  416. if (atomic_test_and_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
  417. &port->status))
  418. zfcp_rec_dbf_event_port(18, NULL, port);
  419. }
  420. /*
  421. * function:
  422. *
  423. * purpose: disable I/O,
  424. * return any open requests and clean them up,
  425. * aim: no pending and incoming I/O
  426. *
  427. * returns:
  428. */
  429. static void
  430. zfcp_erp_unit_block(struct zfcp_unit *unit, int clear_mask)
  431. {
  432. zfcp_erp_modify_unit_status(unit, 19, NULL,
  433. ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
  434. ZFCP_CLEAR);
  435. }
  436. /*
  437. * function:
  438. *
  439. * purpose: enable I/O
  440. *
  441. * returns:
  442. */
  443. static void
  444. zfcp_erp_unit_unblock(struct zfcp_unit *unit)
  445. {
  446. if (atomic_test_and_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
  447. &unit->status))
  448. zfcp_rec_dbf_event_unit(20, NULL, unit);
  449. }
  450. static void
  451. zfcp_erp_action_ready(struct zfcp_erp_action *erp_action)
  452. {
  453. struct zfcp_adapter *adapter = erp_action->adapter;
  454. zfcp_erp_action_to_ready(erp_action);
  455. up(&adapter->erp_ready_sem);
  456. zfcp_rec_dbf_event_thread(2, adapter);
  457. }
  458. /*
  459. * function:
  460. *
  461. * purpose:
  462. *
  463. * returns: <0 erp_action not found in any list
  464. * ZFCP_ERP_ACTION_READY erp_action is in ready list
  465. * ZFCP_ERP_ACTION_RUNNING erp_action is in running list
  466. *
  467. * locks: erp_lock must be held
  468. */
  469. static int
  470. zfcp_erp_action_exists(struct zfcp_erp_action *erp_action)
  471. {
  472. int retval = -EINVAL;
  473. struct list_head *entry;
  474. struct zfcp_erp_action *entry_erp_action;
  475. struct zfcp_adapter *adapter = erp_action->adapter;
  476. /* search in running list */
  477. list_for_each(entry, &adapter->erp_running_head) {
  478. entry_erp_action =
  479. list_entry(entry, struct zfcp_erp_action, list);
  480. if (entry_erp_action == erp_action) {
  481. retval = ZFCP_ERP_ACTION_RUNNING;
  482. goto out;
  483. }
  484. }
  485. /* search in ready list */
  486. list_for_each(entry, &adapter->erp_ready_head) {
  487. entry_erp_action =
  488. list_entry(entry, struct zfcp_erp_action, list);
  489. if (entry_erp_action == erp_action) {
  490. retval = ZFCP_ERP_ACTION_READY;
  491. goto out;
  492. }
  493. }
  494. out:
  495. return retval;
  496. }
  497. /*
  498. * purpose: checks current status of action (timed out, dismissed, ...)
  499. * and does appropriate preparations (dismiss fsf request, ...)
  500. *
  501. * locks: called under erp_lock (disabled interrupts)
  502. */
  503. static void
  504. zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *erp_action)
  505. {
  506. struct zfcp_adapter *adapter = erp_action->adapter;
  507. if (erp_action->fsf_req) {
  508. /* take lock to ensure that request is not deleted meanwhile */
  509. spin_lock(&adapter->req_list_lock);
  510. if (zfcp_reqlist_find_safe(adapter, erp_action->fsf_req) &&
  511. erp_action->fsf_req->erp_action == erp_action) {
  512. /* fsf_req still exists */
  513. /* dismiss fsf_req of timed out/dismissed erp_action */
  514. if (erp_action->status & (ZFCP_STATUS_ERP_DISMISSED |
  515. ZFCP_STATUS_ERP_TIMEDOUT)) {
  516. erp_action->fsf_req->status |=
  517. ZFCP_STATUS_FSFREQ_DISMISSED;
  518. zfcp_rec_dbf_event_action(142, erp_action);
  519. }
  520. if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
  521. zfcp_rec_dbf_event_action(143, erp_action);
  522. /*
  523. * If fsf_req is neither dismissed nor completed
  524. * then keep it running asynchronously and don't mess
  525. * with the association of erp_action and fsf_req.
  526. */
  527. if (erp_action->fsf_req->status &
  528. (ZFCP_STATUS_FSFREQ_COMPLETED |
  529. ZFCP_STATUS_FSFREQ_DISMISSED)) {
  530. /* forget about association between fsf_req
  531. and erp_action */
  532. erp_action->fsf_req = NULL;
  533. }
  534. } else {
  535. /*
  536. * even if this fsf_req has gone, forget about
  537. * association between erp_action and fsf_req
  538. */
  539. erp_action->fsf_req = NULL;
  540. }
  541. spin_unlock(&adapter->req_list_lock);
  542. }
  543. }
  544. /**
  545. * zfcp_erp_async_handler_nolock - complete erp_action
  546. *
  547. * Used for normal completion, time-out, dismissal and failure after
  548. * low memory condition.
  549. */
  550. static void zfcp_erp_async_handler_nolock(struct zfcp_erp_action *erp_action,
  551. unsigned long set_mask)
  552. {
  553. if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
  554. erp_action->status |= set_mask;
  555. zfcp_erp_action_ready(erp_action);
  556. } else {
  557. /* action is ready or gone - nothing to do */
  558. }
  559. }
  560. /**
  561. * zfcp_erp_async_handler - wrapper for erp_async_handler_nolock w/ locking
  562. */
  563. void zfcp_erp_async_handler(struct zfcp_erp_action *erp_action,
  564. unsigned long set_mask)
  565. {
  566. struct zfcp_adapter *adapter = erp_action->adapter;
  567. unsigned long flags;
  568. write_lock_irqsave(&adapter->erp_lock, flags);
  569. zfcp_erp_async_handler_nolock(erp_action, set_mask);
  570. write_unlock_irqrestore(&adapter->erp_lock, flags);
  571. }
  572. /*
  573. * purpose: is called for erp_action which was slept waiting for
  574. * memory becoming avaliable,
  575. * will trigger that this action will be continued
  576. */
  577. static void
  578. zfcp_erp_memwait_handler(unsigned long data)
  579. {
  580. struct zfcp_erp_action *erp_action = (struct zfcp_erp_action *) data;
  581. zfcp_erp_async_handler(erp_action, 0);
  582. }
  583. /*
  584. * purpose: is called if an asynchronous erp step timed out,
  585. * action gets an appropriate flag and will be processed
  586. * accordingly
  587. */
  588. static void zfcp_erp_timeout_handler(unsigned long data)
  589. {
  590. struct zfcp_erp_action *erp_action = (struct zfcp_erp_action *) data;
  591. zfcp_erp_async_handler(erp_action, ZFCP_STATUS_ERP_TIMEDOUT);
  592. }
  593. /**
  594. * zfcp_erp_action_dismiss - dismiss an erp_action
  595. *
  596. * adapter->erp_lock must be held
  597. *
  598. * Dismissal of an erp_action is usually required if an erp_action of
  599. * higher priority is generated.
  600. */
  601. static void zfcp_erp_action_dismiss(struct zfcp_erp_action *erp_action)
  602. {
  603. erp_action->status |= ZFCP_STATUS_ERP_DISMISSED;
  604. if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING)
  605. zfcp_erp_action_ready(erp_action);
  606. }
  607. int
  608. zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
  609. {
  610. int retval = 0;
  611. atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
  612. retval = kernel_thread(zfcp_erp_thread, adapter, SIGCHLD);
  613. if (retval < 0)
  614. dev_err(&adapter->ccw_device->dev,
  615. "Creation of ERP thread failed.\n");
  616. else {
  617. wait_event(adapter->erp_thread_wqh,
  618. atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
  619. &adapter->status));
  620. }
  621. return (retval < 0);
  622. }
  623. /*
  624. * function:
  625. *
  626. * purpose:
  627. *
  628. * returns:
  629. *
  630. * context: process (i.e. proc-fs or rmmod/insmod)
  631. *
  632. * note: The caller of this routine ensures that the specified
  633. * adapter has been shut down and that this operation
  634. * has been completed. Thus, there are no pending erp_actions
  635. * which would need to be handled here.
  636. */
  637. int
  638. zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
  639. {
  640. int retval = 0;
  641. atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL, &adapter->status);
  642. up(&adapter->erp_ready_sem);
  643. zfcp_rec_dbf_event_thread_lock(2, adapter);
  644. wait_event(adapter->erp_thread_wqh,
  645. !atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
  646. &adapter->status));
  647. atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
  648. &adapter->status);
  649. return retval;
  650. }
  651. /*
  652. * purpose: is run as a kernel thread,
  653. * goes through list of error recovery actions of associated adapter
  654. * and delegates single action to execution
  655. *
  656. * returns: 0
  657. */
  658. static int
  659. zfcp_erp_thread(void *data)
  660. {
  661. struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
  662. struct list_head *next;
  663. struct zfcp_erp_action *erp_action;
  664. unsigned long flags;
  665. daemonize("zfcperp%s", zfcp_get_busid_by_adapter(adapter));
  666. /* Block all signals */
  667. siginitsetinv(&current->blocked, 0);
  668. atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
  669. wake_up(&adapter->erp_thread_wqh);
  670. while (!atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
  671. &adapter->status)) {
  672. write_lock_irqsave(&adapter->erp_lock, flags);
  673. next = adapter->erp_ready_head.next;
  674. write_unlock_irqrestore(&adapter->erp_lock, flags);
  675. if (next != &adapter->erp_ready_head) {
  676. erp_action =
  677. list_entry(next, struct zfcp_erp_action, list);
  678. /*
  679. * process action (incl. [re]moving it
  680. * from 'ready' queue)
  681. */
  682. zfcp_erp_strategy(erp_action);
  683. }
  684. /*
  685. * sleep as long as there is nothing to do, i.e.
  686. * no action in 'ready' queue to be processed and
  687. * thread is not to be killed
  688. */
  689. zfcp_rec_dbf_event_thread_lock(4, adapter);
  690. down_interruptible(&adapter->erp_ready_sem);
  691. zfcp_rec_dbf_event_thread_lock(5, adapter);
  692. }
  693. atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
  694. wake_up(&adapter->erp_thread_wqh);
  695. return 0;
  696. }
  697. /*
  698. * function:
  699. *
  700. * purpose: drives single error recovery action and schedules higher and
  701. * subordinate actions, if necessary
  702. *
  703. * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
  704. * ZFCP_ERP_SUCCEEDED - action finished successfully (deqd)
  705. * ZFCP_ERP_FAILED - action finished unsuccessfully (deqd)
  706. * ZFCP_ERP_EXIT - action finished (dequeued), offline
  707. * ZFCP_ERP_DISMISSED - action canceled (dequeued)
  708. */
  709. static int
  710. zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
  711. {
  712. int retval = 0;
  713. struct zfcp_adapter *adapter = erp_action->adapter;
  714. struct zfcp_port *port = erp_action->port;
  715. struct zfcp_unit *unit = erp_action->unit;
  716. int action = erp_action->action;
  717. u32 status = erp_action->status;
  718. unsigned long flags;
  719. /* serialise dismissing, timing out, moving, enqueueing */
  720. read_lock_irqsave(&zfcp_data.config_lock, flags);
  721. write_lock(&adapter->erp_lock);
  722. /* dequeue dismissed action and leave, if required */
  723. retval = zfcp_erp_strategy_check_action(erp_action, retval);
  724. if (retval == ZFCP_ERP_DISMISSED) {
  725. goto unlock;
  726. }
  727. /*
  728. * move action to 'running' queue before processing it
  729. * (to avoid a race condition regarding moving the
  730. * action to the 'running' queue and back)
  731. */
  732. zfcp_erp_action_to_running(erp_action);
  733. /*
  734. * try to process action as far as possible,
  735. * no lock to allow for blocking operations (kmalloc, qdio, ...),
  736. * afterwards the lock is required again for the following reasons:
  737. * - dequeueing of finished action and enqueueing of
  738. * follow-up actions must be atomic so that any other
  739. * reopen-routine does not believe there is nothing to do
  740. * and that it is safe to enqueue something else,
  741. * - we want to force any control thread which is dismissing
  742. * actions to finish this before we decide about
  743. * necessary steps to be taken here further
  744. */
  745. write_unlock(&adapter->erp_lock);
  746. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  747. retval = zfcp_erp_strategy_do_action(erp_action);
  748. read_lock_irqsave(&zfcp_data.config_lock, flags);
  749. write_lock(&adapter->erp_lock);
  750. /*
  751. * check for dismissed status again to avoid follow-up actions,
  752. * failing of targets and so on for dismissed actions,
  753. * we go through down() here because there has been an up()
  754. */
  755. if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
  756. retval = ZFCP_ERP_CONTINUES;
  757. switch (retval) {
  758. case ZFCP_ERP_NOMEM:
  759. /* no memory to continue immediately, let it sleep */
  760. if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
  761. ++adapter->erp_low_mem_count;
  762. erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
  763. }
  764. /* This condition is true if there is no memory available
  765. for any erp_action on this adapter. This implies that there
  766. are no elements in the memory pool(s) left for erp_actions.
  767. This might happen if an erp_action that used a memory pool
  768. element was timed out.
  769. */
  770. if (adapter->erp_total_count == adapter->erp_low_mem_count)
  771. zfcp_erp_adapter_reopen_internal(adapter, 0, 66, NULL);
  772. else
  773. retval = zfcp_erp_strategy_memwait(erp_action);
  774. goto unlock;
  775. case ZFCP_ERP_CONTINUES:
  776. /* leave since this action runs asynchronously */
  777. if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
  778. --adapter->erp_low_mem_count;
  779. erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
  780. }
  781. goto unlock;
  782. }
  783. /* ok, finished action (whatever its result is) */
  784. /* check for unrecoverable targets */
  785. retval = zfcp_erp_strategy_check_target(erp_action, retval);
  786. /* action must be dequeued (here to allow for further ones) */
  787. zfcp_erp_action_dequeue(erp_action);
  788. /*
  789. * put this target through the erp mill again if someone has
  790. * requested to change the status of a target being online
  791. * to offline or the other way around
  792. * (old retval is preserved if nothing has to be done here)
  793. */
  794. retval = zfcp_erp_strategy_statechange(action, status, adapter,
  795. port, unit, retval);
  796. /*
  797. * leave if target is in permanent error state or if
  798. * action is repeated in order to process state change
  799. */
  800. if (retval == ZFCP_ERP_EXIT) {
  801. goto unlock;
  802. }
  803. /* trigger follow up actions */
  804. zfcp_erp_strategy_followup_actions(action, adapter, port, unit, retval);
  805. unlock:
  806. write_unlock(&adapter->erp_lock);
  807. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  808. if (retval != ZFCP_ERP_CONTINUES)
  809. zfcp_erp_action_cleanup(action, adapter, port, unit, retval);
  810. /*
  811. * a few tasks remain when the erp queues are empty
  812. * (don't do that if the last action evaluated was dismissed
  813. * since this clearly indicates that there is more to come) :
  814. * - close the name server port if it is open yet
  815. * (enqueues another [probably] final action)
  816. * - otherwise, wake up whoever wants to be woken when we are
  817. * done with erp
  818. */
  819. if (retval != ZFCP_ERP_DISMISSED)
  820. zfcp_erp_strategy_check_queues(adapter);
  821. return retval;
  822. }
  823. /*
  824. * function:
  825. *
  826. * purpose:
  827. *
  828. * returns: ZFCP_ERP_DISMISSED - if action has been dismissed
  829. * retval - otherwise
  830. */
  831. static int
  832. zfcp_erp_strategy_check_action(struct zfcp_erp_action *erp_action, int retval)
  833. {
  834. zfcp_erp_strategy_check_fsfreq(erp_action);
  835. if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
  836. zfcp_erp_action_dequeue(erp_action);
  837. retval = ZFCP_ERP_DISMISSED;
  838. }
  839. return retval;
  840. }
  841. static int
  842. zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
  843. {
  844. int retval = ZFCP_ERP_FAILED;
  845. /*
  846. * try to execute/continue action as far as possible,
  847. * note: no lock in subsequent strategy routines
  848. * (this allows these routine to call schedule, e.g.
  849. * kmalloc with such flags or qdio_initialize & friends)
  850. * Note: in case of timeout, the separate strategies will fail
  851. * anyhow. No need for a special action. Even worse, a nameserver
  852. * failure would not wake up waiting ports without the call.
  853. */
  854. switch (erp_action->action) {
  855. case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
  856. retval = zfcp_erp_adapter_strategy(erp_action);
  857. break;
  858. case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
  859. retval = zfcp_erp_port_forced_strategy(erp_action);
  860. break;
  861. case ZFCP_ERP_ACTION_REOPEN_PORT:
  862. retval = zfcp_erp_port_strategy(erp_action);
  863. break;
  864. case ZFCP_ERP_ACTION_REOPEN_UNIT:
  865. retval = zfcp_erp_unit_strategy(erp_action);
  866. break;
  867. }
  868. return retval;
  869. }
  870. /*
  871. * function:
  872. *
  873. * purpose: triggers retry of this action after a certain amount of time
  874. * by means of timer provided by erp_action
  875. *
  876. * returns: ZFCP_ERP_CONTINUES - erp_action sleeps in erp running queue
  877. */
  878. static int
  879. zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
  880. {
  881. int retval = ZFCP_ERP_CONTINUES;
  882. init_timer(&erp_action->timer);
  883. erp_action->timer.function = zfcp_erp_memwait_handler;
  884. erp_action->timer.data = (unsigned long) erp_action;
  885. erp_action->timer.expires = jiffies + ZFCP_ERP_MEMWAIT_TIMEOUT;
  886. add_timer(&erp_action->timer);
  887. return retval;
  888. }
  889. /*
  890. * function: zfcp_erp_adapter_failed
  891. *
  892. * purpose: sets the adapter and all underlying devices to ERP_FAILED
  893. *
  894. */
  895. void
  896. zfcp_erp_adapter_failed(struct zfcp_adapter *adapter, u8 id, void *ref)
  897. {
  898. zfcp_erp_modify_adapter_status(adapter, id, ref,
  899. ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
  900. dev_err(&adapter->ccw_device->dev, "Adapter ERP failed.\n");
  901. }
  902. /*
  903. * function: zfcp_erp_port_failed
  904. *
  905. * purpose: sets the port and all underlying devices to ERP_FAILED
  906. *
  907. */
  908. void
  909. zfcp_erp_port_failed(struct zfcp_port *port, u8 id, void *ref)
  910. {
  911. zfcp_erp_modify_port_status(port, id, ref,
  912. ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
  913. if (atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
  914. dev_err(&port->adapter->ccw_device->dev,
  915. "Port ERP failed for WKA port d_id=0x%06x.\n",
  916. port->d_id);
  917. else
  918. dev_err(&port->adapter->ccw_device->dev,
  919. "Port ERP failed for port wwpn=0x%016Lx.\n",
  920. port->wwpn);
  921. }
  922. /*
  923. * function: zfcp_erp_unit_failed
  924. *
  925. * purpose: sets the unit to ERP_FAILED
  926. *
  927. */
  928. void
  929. zfcp_erp_unit_failed(struct zfcp_unit *unit, u8 id, void *ref)
  930. {
  931. zfcp_erp_modify_unit_status(unit, id, ref,
  932. ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
  933. dev_err(&unit->port->adapter->ccw_device->dev,
  934. "Unit ERP failed for unit 0x%016Lx on port 0x%016Lx.\n",
  935. unit->fcp_lun, unit->port->wwpn);
  936. }
  937. /*
  938. * function: zfcp_erp_strategy_check_target
  939. *
  940. * purpose: increments the erp action count on the device currently in
  941. * recovery if the action failed or resets the count in case of
  942. * success. If a maximum count is exceeded the device is marked
  943. * as ERP_FAILED.
  944. * The 'blocked' state of a target which has been recovered
  945. * successfully is reset.
  946. *
  947. * returns: ZFCP_ERP_CONTINUES - action continues (not considered)
  948. * ZFCP_ERP_SUCCEEDED - action finished successfully
  949. * ZFCP_ERP_EXIT - action failed and will not continue
  950. */
  951. static int
  952. zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action, int result)
  953. {
  954. struct zfcp_adapter *adapter = erp_action->adapter;
  955. struct zfcp_port *port = erp_action->port;
  956. struct zfcp_unit *unit = erp_action->unit;
  957. switch (erp_action->action) {
  958. case ZFCP_ERP_ACTION_REOPEN_UNIT:
  959. result = zfcp_erp_strategy_check_unit(unit, result);
  960. break;
  961. case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
  962. case ZFCP_ERP_ACTION_REOPEN_PORT:
  963. result = zfcp_erp_strategy_check_port(port, result);
  964. break;
  965. case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
  966. result = zfcp_erp_strategy_check_adapter(adapter, result);
  967. break;
  968. }
  969. return result;
  970. }
  971. static int
  972. zfcp_erp_strategy_statechange(int action,
  973. u32 status,
  974. struct zfcp_adapter *adapter,
  975. struct zfcp_port *port,
  976. struct zfcp_unit *unit, int retval)
  977. {
  978. switch (action) {
  979. case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
  980. if (zfcp_erp_strategy_statechange_detected(&adapter->status,
  981. status)) {
  982. zfcp_erp_adapter_reopen_internal(adapter,
  983. ZFCP_STATUS_COMMON_ERP_FAILED,
  984. 67, NULL);
  985. retval = ZFCP_ERP_EXIT;
  986. }
  987. break;
  988. case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
  989. case ZFCP_ERP_ACTION_REOPEN_PORT:
  990. if (zfcp_erp_strategy_statechange_detected(&port->status,
  991. status)) {
  992. zfcp_erp_port_reopen_internal(port,
  993. ZFCP_STATUS_COMMON_ERP_FAILED,
  994. 68, NULL);
  995. retval = ZFCP_ERP_EXIT;
  996. }
  997. break;
  998. case ZFCP_ERP_ACTION_REOPEN_UNIT:
  999. if (zfcp_erp_strategy_statechange_detected(&unit->status,
  1000. status)) {
  1001. zfcp_erp_unit_reopen_internal(unit,
  1002. ZFCP_STATUS_COMMON_ERP_FAILED,
  1003. 69, NULL);
  1004. retval = ZFCP_ERP_EXIT;
  1005. }
  1006. break;
  1007. }
  1008. return retval;
  1009. }
  1010. static int
  1011. zfcp_erp_strategy_statechange_detected(atomic_t * target_status, u32 erp_status)
  1012. {
  1013. return
  1014. /* take it online */
  1015. (atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, target_status) &&
  1016. (ZFCP_STATUS_ERP_CLOSE_ONLY & erp_status)) ||
  1017. /* take it offline */
  1018. (!atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, target_status) &&
  1019. !(ZFCP_STATUS_ERP_CLOSE_ONLY & erp_status));
  1020. }
  1021. static int
  1022. zfcp_erp_strategy_check_unit(struct zfcp_unit *unit, int result)
  1023. {
  1024. switch (result) {
  1025. case ZFCP_ERP_SUCCEEDED :
  1026. atomic_set(&unit->erp_counter, 0);
  1027. zfcp_erp_unit_unblock(unit);
  1028. break;
  1029. case ZFCP_ERP_FAILED :
  1030. atomic_inc(&unit->erp_counter);
  1031. if (atomic_read(&unit->erp_counter) > ZFCP_MAX_ERPS)
  1032. zfcp_erp_unit_failed(unit, 21, NULL);
  1033. break;
  1034. case ZFCP_ERP_EXIT :
  1035. /* nothing */
  1036. break;
  1037. }
  1038. if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status)) {
  1039. zfcp_erp_unit_block(unit, 0); /* for ZFCP_ERP_SUCCEEDED */
  1040. result = ZFCP_ERP_EXIT;
  1041. }
  1042. return result;
  1043. }
  1044. static int
  1045. zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
  1046. {
  1047. switch (result) {
  1048. case ZFCP_ERP_SUCCEEDED :
  1049. atomic_set(&port->erp_counter, 0);
  1050. zfcp_erp_port_unblock(port);
  1051. break;
  1052. case ZFCP_ERP_FAILED :
  1053. if (atomic_test_mask(ZFCP_STATUS_COMMON_NOESC, &port->status)) {
  1054. zfcp_erp_port_block(port, 0);
  1055. result = ZFCP_ERP_EXIT;
  1056. }
  1057. atomic_inc(&port->erp_counter);
  1058. if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS)
  1059. zfcp_erp_port_failed(port, 22, NULL);
  1060. break;
  1061. case ZFCP_ERP_EXIT :
  1062. /* nothing */
  1063. break;
  1064. }
  1065. if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
  1066. zfcp_erp_port_block(port, 0); /* for ZFCP_ERP_SUCCEEDED */
  1067. result = ZFCP_ERP_EXIT;
  1068. }
  1069. return result;
  1070. }
  1071. static int
  1072. zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter, int result)
  1073. {
  1074. switch (result) {
  1075. case ZFCP_ERP_SUCCEEDED :
  1076. atomic_set(&adapter->erp_counter, 0);
  1077. zfcp_erp_adapter_unblock(adapter);
  1078. break;
  1079. case ZFCP_ERP_FAILED :
  1080. atomic_inc(&adapter->erp_counter);
  1081. if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS)
  1082. zfcp_erp_adapter_failed(adapter, 23, NULL);
  1083. break;
  1084. case ZFCP_ERP_EXIT :
  1085. /* nothing */
  1086. break;
  1087. }
  1088. if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
  1089. zfcp_erp_adapter_block(adapter, 0); /* for ZFCP_ERP_SUCCEEDED */
  1090. result = ZFCP_ERP_EXIT;
  1091. }
  1092. return result;
  1093. }
  1094. struct zfcp_erp_add_work {
  1095. struct zfcp_unit *unit;
  1096. struct work_struct work;
  1097. };
  1098. /**
  1099. * zfcp_erp_scsi_scan
  1100. * @data: pointer to a struct zfcp_erp_add_work
  1101. *
  1102. * Registers a logical unit with the SCSI stack.
  1103. */
  1104. static void zfcp_erp_scsi_scan(struct work_struct *work)
  1105. {
  1106. struct zfcp_erp_add_work *p =
  1107. container_of(work, struct zfcp_erp_add_work, work);
  1108. struct zfcp_unit *unit = p->unit;
  1109. struct fc_rport *rport = unit->port->rport;
  1110. scsi_scan_target(&rport->dev, 0, rport->scsi_target_id,
  1111. unit->scsi_lun, 0);
  1112. atomic_clear_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING, &unit->status);
  1113. zfcp_unit_put(unit);
  1114. kfree(p);
  1115. }
  1116. /**
  1117. * zfcp_erp_schedule_work
  1118. * @unit: pointer to unit which should be registered with SCSI stack
  1119. *
  1120. * Schedules work which registers a unit with the SCSI stack
  1121. */
  1122. static void
  1123. zfcp_erp_schedule_work(struct zfcp_unit *unit)
  1124. {
  1125. struct zfcp_erp_add_work *p;
  1126. p = kzalloc(sizeof(*p), GFP_KERNEL);
  1127. if (!p) {
  1128. dev_err(&unit->port->adapter->ccw_device->dev,
  1129. "Out of resources. Could not register unit 0x%016Lx "
  1130. "on port 0x%016Lx with SCSI stack.\n",
  1131. unit->fcp_lun, unit->port->wwpn);
  1132. return;
  1133. }
  1134. zfcp_unit_get(unit);
  1135. atomic_set_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING, &unit->status);
  1136. INIT_WORK(&p->work, zfcp_erp_scsi_scan);
  1137. p->unit = unit;
  1138. schedule_work(&p->work);
  1139. }
  1140. /*
  1141. * function:
  1142. *
  1143. * purpose: remaining things in good cases,
  1144. * escalation in bad cases
  1145. *
  1146. * returns:
  1147. */
  1148. static int
  1149. zfcp_erp_strategy_followup_actions(int action,
  1150. struct zfcp_adapter *adapter,
  1151. struct zfcp_port *port,
  1152. struct zfcp_unit *unit, int status)
  1153. {
  1154. /* initiate follow-up actions depending on success of finished action */
  1155. switch (action) {
  1156. case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
  1157. if (status == ZFCP_ERP_SUCCEEDED)
  1158. zfcp_erp_port_reopen_all_internal(adapter, 0, 70, NULL);
  1159. else
  1160. zfcp_erp_adapter_reopen_internal(adapter, 0, 71, NULL);
  1161. break;
  1162. case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
  1163. if (status == ZFCP_ERP_SUCCEEDED)
  1164. zfcp_erp_port_reopen_internal(port, 0, 72, NULL);
  1165. else
  1166. zfcp_erp_adapter_reopen_internal(adapter, 0, 73, NULL);
  1167. break;
  1168. case ZFCP_ERP_ACTION_REOPEN_PORT:
  1169. if (status == ZFCP_ERP_SUCCEEDED)
  1170. zfcp_erp_unit_reopen_all_internal(port, 0, 74, NULL);
  1171. else
  1172. zfcp_erp_port_forced_reopen_internal(port, 0, 75, NULL);
  1173. break;
  1174. case ZFCP_ERP_ACTION_REOPEN_UNIT:
  1175. /* Nothing to do if status == ZFCP_ERP_SUCCEEDED */
  1176. if (status != ZFCP_ERP_SUCCEEDED)
  1177. zfcp_erp_port_reopen_internal(unit->port, 0, 76, NULL);
  1178. break;
  1179. }
  1180. return 0;
  1181. }
  1182. static int
  1183. zfcp_erp_strategy_check_queues(struct zfcp_adapter *adapter)
  1184. {
  1185. unsigned long flags;
  1186. read_lock_irqsave(&zfcp_data.config_lock, flags);
  1187. read_lock(&adapter->erp_lock);
  1188. if (list_empty(&adapter->erp_ready_head) &&
  1189. list_empty(&adapter->erp_running_head)) {
  1190. atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
  1191. &adapter->status);
  1192. wake_up(&adapter->erp_done_wqh);
  1193. }
  1194. read_unlock(&adapter->erp_lock);
  1195. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  1196. return 0;
  1197. }
  1198. /**
  1199. * zfcp_erp_wait - wait for completion of error recovery on an adapter
  1200. * @adapter: adapter for which to wait for completion of its error recovery
  1201. * Return: 0
  1202. */
  1203. int
  1204. zfcp_erp_wait(struct zfcp_adapter *adapter)
  1205. {
  1206. int retval = 0;
  1207. wait_event(adapter->erp_done_wqh,
  1208. !atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
  1209. &adapter->status));
  1210. return retval;
  1211. }
  1212. void zfcp_erp_modify_adapter_status(struct zfcp_adapter *adapter, u8 id,
  1213. void *ref, u32 mask, int set_or_clear)
  1214. {
  1215. struct zfcp_port *port;
  1216. u32 changed, common_mask = mask & ZFCP_COMMON_FLAGS;
  1217. if (set_or_clear == ZFCP_SET) {
  1218. changed = atomic_test_and_set_mask(mask, &adapter->status);
  1219. } else {
  1220. changed = atomic_test_and_clear_mask(mask, &adapter->status);
  1221. if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
  1222. atomic_set(&adapter->erp_counter, 0);
  1223. }
  1224. if (changed)
  1225. zfcp_rec_dbf_event_adapter(id, ref, adapter);
  1226. /* Deal with all underlying devices, only pass common_mask */
  1227. if (common_mask)
  1228. list_for_each_entry(port, &adapter->port_list_head, list)
  1229. zfcp_erp_modify_port_status(port, id, ref, common_mask,
  1230. set_or_clear);
  1231. }
  1232. /*
  1233. * function: zfcp_erp_modify_port_status
  1234. *
  1235. * purpose: sets the port and all underlying devices to ERP_FAILED
  1236. *
  1237. */
  1238. void zfcp_erp_modify_port_status(struct zfcp_port *port, u8 id, void *ref,
  1239. u32 mask, int set_or_clear)
  1240. {
  1241. struct zfcp_unit *unit;
  1242. u32 changed, common_mask = mask & ZFCP_COMMON_FLAGS;
  1243. if (set_or_clear == ZFCP_SET) {
  1244. changed = atomic_test_and_set_mask(mask, &port->status);
  1245. } else {
  1246. changed = atomic_test_and_clear_mask(mask, &port->status);
  1247. if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
  1248. atomic_set(&port->erp_counter, 0);
  1249. }
  1250. if (changed)
  1251. zfcp_rec_dbf_event_port(id, ref, port);
  1252. /* Modify status of all underlying devices, only pass common mask */
  1253. if (common_mask)
  1254. list_for_each_entry(unit, &port->unit_list_head, list)
  1255. zfcp_erp_modify_unit_status(unit, id, ref, common_mask,
  1256. set_or_clear);
  1257. }
  1258. /*
  1259. * function: zfcp_erp_modify_unit_status
  1260. *
  1261. * purpose: sets the unit to ERP_FAILED
  1262. *
  1263. */
  1264. void zfcp_erp_modify_unit_status(struct zfcp_unit *unit, u8 id, void *ref,
  1265. u32 mask, int set_or_clear)
  1266. {
  1267. u32 changed;
  1268. if (set_or_clear == ZFCP_SET) {
  1269. changed = atomic_test_and_set_mask(mask, &unit->status);
  1270. } else {
  1271. changed = atomic_test_and_clear_mask(mask, &unit->status);
  1272. if (mask & ZFCP_STATUS_COMMON_ERP_FAILED) {
  1273. atomic_set(&unit->erp_counter, 0);
  1274. }
  1275. }
  1276. if (changed)
  1277. zfcp_rec_dbf_event_unit(id, ref, unit);
  1278. }
  1279. /*
  1280. * function:
  1281. *
  1282. * purpose: Wrappper for zfcp_erp_port_reopen_all_internal
  1283. * used to ensure the correct locking
  1284. *
  1285. * returns: 0 - initiated action successfully
  1286. * <0 - failed to initiate action
  1287. */
  1288. int zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter, int clear_mask,
  1289. u8 id, void *ref)
  1290. {
  1291. int retval;
  1292. unsigned long flags;
  1293. read_lock_irqsave(&zfcp_data.config_lock, flags);
  1294. write_lock(&adapter->erp_lock);
  1295. retval = zfcp_erp_port_reopen_all_internal(adapter, clear_mask, id,
  1296. ref);
  1297. write_unlock(&adapter->erp_lock);
  1298. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  1299. return retval;
  1300. }
  1301. static int zfcp_erp_port_reopen_all_internal(struct zfcp_adapter *adapter,
  1302. int clear_mask, u8 id, void *ref)
  1303. {
  1304. int retval = 0;
  1305. struct zfcp_port *port;
  1306. list_for_each_entry(port, &adapter->port_list_head, list)
  1307. if (!atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
  1308. zfcp_erp_port_reopen_internal(port, clear_mask, id,
  1309. ref);
  1310. return retval;
  1311. }
  1312. /*
  1313. * function:
  1314. *
  1315. * purpose:
  1316. *
  1317. * returns: FIXME
  1318. */
  1319. static int zfcp_erp_unit_reopen_all_internal(struct zfcp_port *port,
  1320. int clear_mask, u8 id, void *ref)
  1321. {
  1322. int retval = 0;
  1323. struct zfcp_unit *unit;
  1324. list_for_each_entry(unit, &port->unit_list_head, list)
  1325. zfcp_erp_unit_reopen_internal(unit, clear_mask, id, ref);
  1326. return retval;
  1327. }
  1328. /*
  1329. * function:
  1330. *
  1331. * purpose: this routine executes the 'Reopen Adapter' action
  1332. * (the entire action is processed synchronously, since
  1333. * there are no actions which might be run concurrently
  1334. * per definition)
  1335. *
  1336. * returns: ZFCP_ERP_SUCCEEDED - action finished successfully
  1337. * ZFCP_ERP_FAILED - action finished unsuccessfully
  1338. */
  1339. static int
  1340. zfcp_erp_adapter_strategy(struct zfcp_erp_action *erp_action)
  1341. {
  1342. int retval;
  1343. retval = zfcp_erp_adapter_strategy_close(erp_action);
  1344. if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
  1345. retval = ZFCP_ERP_EXIT;
  1346. else
  1347. retval = zfcp_erp_adapter_strategy_open(erp_action);
  1348. if (retval == ZFCP_ERP_FAILED)
  1349. ssleep(ZFCP_TYPE2_RECOVERY_TIME);
  1350. return retval;
  1351. }
  1352. /*
  1353. * function:
  1354. *
  1355. * purpose:
  1356. *
  1357. * returns: ZFCP_ERP_SUCCEEDED - action finished successfully
  1358. * ZFCP_ERP_FAILED - action finished unsuccessfully
  1359. */
  1360. static int
  1361. zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *erp_action)
  1362. {
  1363. int retval;
  1364. atomic_set_mask(ZFCP_STATUS_COMMON_CLOSING,
  1365. &erp_action->adapter->status);
  1366. retval = zfcp_erp_adapter_strategy_generic(erp_action, 1);
  1367. atomic_clear_mask(ZFCP_STATUS_COMMON_CLOSING,
  1368. &erp_action->adapter->status);
  1369. return retval;
  1370. }
  1371. /*
  1372. * function:
  1373. *
  1374. * purpose:
  1375. *
  1376. * returns: ZFCP_ERP_SUCCEEDED - action finished successfully
  1377. * ZFCP_ERP_FAILED - action finished unsuccessfully
  1378. */
  1379. static int
  1380. zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *erp_action)
  1381. {
  1382. int retval;
  1383. atomic_set_mask(ZFCP_STATUS_COMMON_OPENING,
  1384. &erp_action->adapter->status);
  1385. retval = zfcp_erp_adapter_strategy_generic(erp_action, 0);
  1386. atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING,
  1387. &erp_action->adapter->status);
  1388. return retval;
  1389. }
  1390. /*
  1391. * function: zfcp_register_adapter
  1392. *
  1393. * purpose: allocate the irq associated with this devno and register
  1394. * the FSF adapter with the SCSI stack
  1395. *
  1396. * returns:
  1397. */
  1398. static int
  1399. zfcp_erp_adapter_strategy_generic(struct zfcp_erp_action *erp_action, int close)
  1400. {
  1401. int retval = ZFCP_ERP_SUCCEEDED;
  1402. if (close)
  1403. goto close_only;
  1404. retval = zfcp_erp_adapter_strategy_open_qdio(erp_action);
  1405. if (retval != ZFCP_ERP_SUCCEEDED)
  1406. goto failed_qdio;
  1407. retval = zfcp_erp_adapter_strategy_open_fsf(erp_action);
  1408. if (retval != ZFCP_ERP_SUCCEEDED)
  1409. goto failed_openfcp;
  1410. atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &erp_action->adapter->status);
  1411. schedule_work(&erp_action->adapter->scan_work);
  1412. goto out;
  1413. close_only:
  1414. atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
  1415. &erp_action->adapter->status);
  1416. failed_openfcp:
  1417. zfcp_close_fsf(erp_action->adapter);
  1418. failed_qdio:
  1419. atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
  1420. ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
  1421. ZFCP_STATUS_ADAPTER_XPORT_OK,
  1422. &erp_action->adapter->status);
  1423. out:
  1424. return retval;
  1425. }
  1426. /*
  1427. * function: zfcp_qdio_init
  1428. *
  1429. * purpose: setup QDIO operation for specified adapter
  1430. *
  1431. * returns: 0 - successful setup
  1432. * !0 - failed setup
  1433. */
  1434. static int
  1435. zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *erp_action)
  1436. {
  1437. struct zfcp_adapter *adapter = erp_action->adapter;
  1438. if (zfcp_qdio_open(adapter))
  1439. return ZFCP_ERP_FAILED;
  1440. /* initialize waitqueue used to wait for free SBALs in requests queue */
  1441. init_waitqueue_head(&adapter->request_wq);
  1442. /* ok, we did it - skip all cleanups for different failures */
  1443. atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
  1444. return ZFCP_ERP_SUCCEEDED;
  1445. }
  1446. static int
  1447. zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *erp_action)
  1448. {
  1449. int retval;
  1450. retval = zfcp_erp_adapter_strategy_open_fsf_xconfig(erp_action);
  1451. if (retval == ZFCP_ERP_FAILED)
  1452. return ZFCP_ERP_FAILED;
  1453. retval = zfcp_erp_adapter_strategy_open_fsf_xport(erp_action);
  1454. if (retval == ZFCP_ERP_FAILED)
  1455. return ZFCP_ERP_FAILED;
  1456. return zfcp_erp_adapter_strategy_open_fsf_statusread(erp_action);
  1457. }
  1458. static void zfcp_erp_open_ptp_port(struct zfcp_adapter *adapter)
  1459. {
  1460. struct zfcp_port *port;
  1461. port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
  1462. adapter->peer_d_id);
  1463. if (IS_ERR(port)) /* error or port already attached */
  1464. return;
  1465. zfcp_erp_port_reopen_internal(port, 0, 150, NULL);
  1466. }
  1467. static int
  1468. zfcp_erp_adapter_strategy_open_fsf_xconfig(struct zfcp_erp_action *erp_action)
  1469. {
  1470. int retries;
  1471. int sleep = ZFCP_EXCHANGE_CONFIG_DATA_FIRST_SLEEP;
  1472. struct zfcp_adapter *adapter = erp_action->adapter;
  1473. atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
  1474. for (retries = ZFCP_EXCHANGE_CONFIG_DATA_RETRIES; retries; retries--) {
  1475. atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
  1476. &adapter->status);
  1477. write_lock_irq(&adapter->erp_lock);
  1478. zfcp_erp_action_to_running(erp_action);
  1479. write_unlock_irq(&adapter->erp_lock);
  1480. if (zfcp_fsf_exchange_config_data(erp_action)) {
  1481. atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
  1482. &adapter->status);
  1483. return ZFCP_ERP_FAILED;
  1484. }
  1485. /*
  1486. * Why this works:
  1487. * Both the normal completion handler as well as the timeout
  1488. * handler will do an 'up' when the 'exchange config data'
  1489. * request completes or times out. Thus, the signal to go on
  1490. * won't be lost utilizing this semaphore.
  1491. * Furthermore, this 'adapter_reopen' action is
  1492. * guaranteed to be the only action being there (highest action
  1493. * which prevents other actions from being created).
  1494. * Resulting from that, the wake signal recognized here
  1495. * _must_ be the one belonging to the 'exchange config
  1496. * data' request.
  1497. */
  1498. zfcp_rec_dbf_event_thread_lock(6, adapter);
  1499. down(&adapter->erp_ready_sem);
  1500. zfcp_rec_dbf_event_thread_lock(7, adapter);
  1501. if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
  1502. break;
  1503. if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
  1504. &adapter->status))
  1505. break;
  1506. /* sleep a little bit before retry */
  1507. ssleep(sleep);
  1508. sleep *= 2;
  1509. }
  1510. atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
  1511. &adapter->status);
  1512. if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
  1513. &adapter->status))
  1514. return ZFCP_ERP_FAILED;
  1515. if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
  1516. zfcp_erp_open_ptp_port(adapter);
  1517. return ZFCP_ERP_SUCCEEDED;
  1518. }
  1519. static int
  1520. zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *erp_action)
  1521. {
  1522. int ret;
  1523. struct zfcp_adapter *adapter;
  1524. adapter = erp_action->adapter;
  1525. atomic_clear_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status);
  1526. write_lock_irq(&adapter->erp_lock);
  1527. zfcp_erp_action_to_running(erp_action);
  1528. write_unlock_irq(&adapter->erp_lock);
  1529. ret = zfcp_fsf_exchange_port_data(erp_action);
  1530. if (ret == -EOPNOTSUPP) {
  1531. return ZFCP_ERP_SUCCEEDED;
  1532. } else if (ret) {
  1533. return ZFCP_ERP_FAILED;
  1534. }
  1535. ret = ZFCP_ERP_SUCCEEDED;
  1536. zfcp_rec_dbf_event_thread_lock(8, adapter);
  1537. down(&adapter->erp_ready_sem);
  1538. zfcp_rec_dbf_event_thread_lock(9, adapter);
  1539. if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
  1540. ret = ZFCP_ERP_FAILED;
  1541. return ret;
  1542. }
  1543. static int
  1544. zfcp_erp_adapter_strategy_open_fsf_statusread(struct zfcp_erp_action
  1545. *erp_action)
  1546. {
  1547. struct zfcp_adapter *adapter = erp_action->adapter;
  1548. atomic_set(&adapter->stat_miss, 16);
  1549. return zfcp_status_read_refill(adapter);
  1550. }
  1551. /*
  1552. * function:
  1553. *
  1554. * purpose: this routine executes the 'Reopen Physical Port' action
  1555. *
  1556. * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
  1557. * ZFCP_ERP_SUCCEEDED - action finished successfully
  1558. * ZFCP_ERP_FAILED - action finished unsuccessfully
  1559. */
  1560. static int
  1561. zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
  1562. {
  1563. int retval = ZFCP_ERP_FAILED;
  1564. struct zfcp_port *port = erp_action->port;
  1565. switch (erp_action->step) {
  1566. /*
  1567. * FIXME:
  1568. * the ULP spec. begs for waiting for oustanding commands
  1569. */
  1570. case ZFCP_ERP_STEP_UNINITIALIZED:
  1571. zfcp_erp_port_strategy_clearstati(port);
  1572. /*
  1573. * it would be sufficient to test only the normal open flag
  1574. * since the phys. open flag cannot be set if the normal
  1575. * open flag is unset - however, this is for readabilty ...
  1576. */
  1577. if (atomic_test_mask((ZFCP_STATUS_PORT_PHYS_OPEN |
  1578. ZFCP_STATUS_COMMON_OPEN),
  1579. &port->status)) {
  1580. retval =
  1581. zfcp_erp_port_forced_strategy_close(erp_action);
  1582. } else
  1583. retval = ZFCP_ERP_FAILED;
  1584. break;
  1585. case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
  1586. if (atomic_test_mask(ZFCP_STATUS_PORT_PHYS_OPEN,
  1587. &port->status)) {
  1588. retval = ZFCP_ERP_FAILED;
  1589. } else
  1590. retval = ZFCP_ERP_SUCCEEDED;
  1591. break;
  1592. }
  1593. return retval;
  1594. }
  1595. /*
  1596. * function:
  1597. *
  1598. * purpose: this routine executes the 'Reopen Port' action
  1599. *
  1600. * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
  1601. * ZFCP_ERP_SUCCEEDED - action finished successfully
  1602. * ZFCP_ERP_FAILED - action finished unsuccessfully
  1603. */
  1604. static int
  1605. zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
  1606. {
  1607. int retval = ZFCP_ERP_FAILED;
  1608. struct zfcp_port *port = erp_action->port;
  1609. switch (erp_action->step) {
  1610. /*
  1611. * FIXME:
  1612. * the ULP spec. begs for waiting for oustanding commands
  1613. */
  1614. case ZFCP_ERP_STEP_UNINITIALIZED:
  1615. zfcp_erp_port_strategy_clearstati(port);
  1616. if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
  1617. retval = zfcp_erp_port_strategy_close(erp_action);
  1618. goto out;
  1619. } /* else it's already closed, open it */
  1620. break;
  1621. case ZFCP_ERP_STEP_PORT_CLOSING:
  1622. if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
  1623. retval = ZFCP_ERP_FAILED;
  1624. goto out;
  1625. } /* else it's closed now, open it */
  1626. break;
  1627. }
  1628. if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
  1629. retval = ZFCP_ERP_EXIT;
  1630. else
  1631. retval = zfcp_erp_port_strategy_open(erp_action);
  1632. out:
  1633. return retval;
  1634. }
  1635. static int
  1636. zfcp_erp_port_strategy_open(struct zfcp_erp_action *erp_action)
  1637. {
  1638. int retval;
  1639. if (atomic_test_mask(ZFCP_STATUS_PORT_WKA,
  1640. &erp_action->port->status))
  1641. retval = zfcp_erp_port_strategy_open_nameserver(erp_action);
  1642. else
  1643. retval = zfcp_erp_port_strategy_open_common(erp_action);
  1644. return retval;
  1645. }
  1646. static int
  1647. zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *erp_action)
  1648. {
  1649. int retval = 0;
  1650. struct zfcp_adapter *adapter = erp_action->adapter;
  1651. struct zfcp_port *port = erp_action->port;
  1652. switch (erp_action->step) {
  1653. case ZFCP_ERP_STEP_UNINITIALIZED:
  1654. case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
  1655. case ZFCP_ERP_STEP_PORT_CLOSING:
  1656. if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP) {
  1657. if (port->wwpn != adapter->peer_wwpn) {
  1658. dev_err(&adapter->ccw_device->dev,
  1659. "Failed to open port 0x%016Lx, "
  1660. "Peer WWPN 0x%016Lx does not match.\n",
  1661. port->wwpn, adapter->peer_wwpn);
  1662. zfcp_erp_port_failed(port, 25, NULL);
  1663. retval = ZFCP_ERP_FAILED;
  1664. break;
  1665. }
  1666. port->d_id = adapter->peer_d_id;
  1667. atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
  1668. retval = zfcp_erp_port_strategy_open_port(erp_action);
  1669. break;
  1670. }
  1671. if (!adapter->nameserver_port) {
  1672. dev_err(&adapter->ccw_device->dev,
  1673. "Nameserver port unavailable.\n");
  1674. retval = ZFCP_ERP_FAILED;
  1675. break;
  1676. }
  1677. if (!atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
  1678. &adapter->nameserver_port->status)) {
  1679. /* nameserver port may live again */
  1680. atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING,
  1681. &adapter->nameserver_port->status);
  1682. if (zfcp_erp_port_reopen(adapter->nameserver_port, 0,
  1683. 77, erp_action) >= 0) {
  1684. erp_action->step =
  1685. ZFCP_ERP_STEP_NAMESERVER_OPEN;
  1686. retval = ZFCP_ERP_CONTINUES;
  1687. } else
  1688. retval = ZFCP_ERP_FAILED;
  1689. break;
  1690. }
  1691. /* else nameserver port is already open, fall through */
  1692. case ZFCP_ERP_STEP_NAMESERVER_OPEN:
  1693. if (!atomic_test_mask(ZFCP_STATUS_COMMON_OPEN,
  1694. &adapter->nameserver_port->status))
  1695. retval = ZFCP_ERP_FAILED;
  1696. else
  1697. retval = zfcp_erp_port_strategy_open_common_lookup
  1698. (erp_action);
  1699. break;
  1700. case ZFCP_ERP_STEP_NAMESERVER_LOOKUP:
  1701. if (!atomic_test_mask(ZFCP_STATUS_PORT_DID_DID, &port->status)) {
  1702. if (atomic_test_mask
  1703. (ZFCP_STATUS_PORT_INVALID_WWPN, &port->status)) {
  1704. zfcp_erp_port_failed(port, 26, NULL);
  1705. retval = ZFCP_ERP_EXIT;
  1706. } else
  1707. retval = ZFCP_ERP_FAILED;
  1708. } else
  1709. retval = zfcp_erp_port_strategy_open_port(erp_action);
  1710. break;
  1711. case ZFCP_ERP_STEP_PORT_OPENING:
  1712. /* D_ID might have changed during open */
  1713. if (atomic_test_mask((ZFCP_STATUS_COMMON_OPEN |
  1714. ZFCP_STATUS_PORT_DID_DID),
  1715. &port->status))
  1716. retval = ZFCP_ERP_SUCCEEDED;
  1717. else
  1718. retval = ZFCP_ERP_FAILED;
  1719. break;
  1720. default:
  1721. /* unknown erp step */
  1722. retval = ZFCP_ERP_FAILED;
  1723. }
  1724. return retval;
  1725. }
  1726. static int
  1727. zfcp_erp_port_strategy_open_nameserver(struct zfcp_erp_action *erp_action)
  1728. {
  1729. int retval;
  1730. struct zfcp_port *port = erp_action->port;
  1731. switch (erp_action->step) {
  1732. case ZFCP_ERP_STEP_UNINITIALIZED:
  1733. case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
  1734. case ZFCP_ERP_STEP_PORT_CLOSING:
  1735. retval = zfcp_erp_port_strategy_open_port(erp_action);
  1736. break;
  1737. case ZFCP_ERP_STEP_PORT_OPENING:
  1738. if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status))
  1739. retval = ZFCP_ERP_SUCCEEDED;
  1740. else
  1741. retval = ZFCP_ERP_FAILED;
  1742. /* this is needed anyway (dont care for retval of wakeup) */
  1743. zfcp_erp_port_strategy_open_nameserver_wakeup(erp_action);
  1744. break;
  1745. default:
  1746. /* unknown erp step */
  1747. retval = ZFCP_ERP_FAILED;
  1748. }
  1749. return retval;
  1750. }
  1751. /*
  1752. * function:
  1753. *
  1754. * purpose: makes the erp thread continue with reopen (physical) port
  1755. * actions which have been paused until the name server port
  1756. * is opened (or failed)
  1757. *
  1758. * returns: 0 (a kind of void retval, its not used)
  1759. */
  1760. static int
  1761. zfcp_erp_port_strategy_open_nameserver_wakeup(struct zfcp_erp_action
  1762. *ns_erp_action)
  1763. {
  1764. int retval = 0;
  1765. unsigned long flags;
  1766. struct zfcp_adapter *adapter = ns_erp_action->adapter;
  1767. struct zfcp_erp_action *erp_action, *tmp;
  1768. read_lock_irqsave(&adapter->erp_lock, flags);
  1769. list_for_each_entry_safe(erp_action, tmp, &adapter->erp_running_head,
  1770. list) {
  1771. if (erp_action->step == ZFCP_ERP_STEP_NAMESERVER_OPEN) {
  1772. if (atomic_test_mask(
  1773. ZFCP_STATUS_COMMON_ERP_FAILED,
  1774. &adapter->nameserver_port->status))
  1775. zfcp_erp_port_failed(erp_action->port, 27,
  1776. NULL);
  1777. zfcp_erp_action_ready(erp_action);
  1778. }
  1779. }
  1780. read_unlock_irqrestore(&adapter->erp_lock, flags);
  1781. return retval;
  1782. }
  1783. /*
  1784. * function:
  1785. *
  1786. * purpose:
  1787. *
  1788. * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
  1789. * ZFCP_ERP_FAILED - action finished unsuccessfully
  1790. */
  1791. static int
  1792. zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *erp_action)
  1793. {
  1794. int retval;
  1795. retval = zfcp_fsf_close_physical_port(erp_action);
  1796. if (retval == -ENOMEM) {
  1797. retval = ZFCP_ERP_NOMEM;
  1798. goto out;
  1799. }
  1800. erp_action->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
  1801. if (retval != 0) {
  1802. /* could not send 'open', fail */
  1803. retval = ZFCP_ERP_FAILED;
  1804. goto out;
  1805. }
  1806. retval = ZFCP_ERP_CONTINUES;
  1807. out:
  1808. return retval;
  1809. }
  1810. static int
  1811. zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
  1812. {
  1813. int retval = 0;
  1814. atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING |
  1815. ZFCP_STATUS_COMMON_CLOSING |
  1816. ZFCP_STATUS_COMMON_ACCESS_DENIED |
  1817. ZFCP_STATUS_PORT_DID_DID |
  1818. ZFCP_STATUS_PORT_PHYS_CLOSING |
  1819. ZFCP_STATUS_PORT_INVALID_WWPN,
  1820. &port->status);
  1821. return retval;
  1822. }
  1823. /*
  1824. * function:
  1825. *
  1826. * purpose:
  1827. *
  1828. * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
  1829. * ZFCP_ERP_FAILED - action finished unsuccessfully
  1830. */
  1831. static int
  1832. zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
  1833. {
  1834. int retval;
  1835. retval = zfcp_fsf_close_port(erp_action);
  1836. if (retval == -ENOMEM) {
  1837. retval = ZFCP_ERP_NOMEM;
  1838. goto out;
  1839. }
  1840. erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
  1841. if (retval != 0) {
  1842. /* could not send 'close', fail */
  1843. retval = ZFCP_ERP_FAILED;
  1844. goto out;
  1845. }
  1846. retval = ZFCP_ERP_CONTINUES;
  1847. out:
  1848. return retval;
  1849. }
  1850. /*
  1851. * function:
  1852. *
  1853. * purpose:
  1854. *
  1855. * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
  1856. * ZFCP_ERP_FAILED - action finished unsuccessfully
  1857. */
  1858. static int
  1859. zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
  1860. {
  1861. int retval;
  1862. retval = zfcp_fsf_open_port(erp_action);
  1863. if (retval == -ENOMEM) {
  1864. retval = ZFCP_ERP_NOMEM;
  1865. goto out;
  1866. }
  1867. erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
  1868. if (retval != 0) {
  1869. /* could not send 'open', fail */
  1870. retval = ZFCP_ERP_FAILED;
  1871. goto out;
  1872. }
  1873. retval = ZFCP_ERP_CONTINUES;
  1874. out:
  1875. return retval;
  1876. }
  1877. /*
  1878. * function:
  1879. *
  1880. * purpose:
  1881. *
  1882. * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
  1883. * ZFCP_ERP_FAILED - action finished unsuccessfully
  1884. */
  1885. static int
  1886. zfcp_erp_port_strategy_open_common_lookup(struct zfcp_erp_action *erp_action)
  1887. {
  1888. int retval;
  1889. retval = zfcp_fc_ns_gid_pn_request(erp_action);
  1890. if (retval == -ENOMEM) {
  1891. retval = ZFCP_ERP_NOMEM;
  1892. goto out;
  1893. }
  1894. erp_action->step = ZFCP_ERP_STEP_NAMESERVER_LOOKUP;
  1895. if (retval != 0) {
  1896. /* could not send nameserver request, fail */
  1897. retval = ZFCP_ERP_FAILED;
  1898. goto out;
  1899. }
  1900. retval = ZFCP_ERP_CONTINUES;
  1901. out:
  1902. return retval;
  1903. }
  1904. /*
  1905. * function:
  1906. *
  1907. * purpose: this routine executes the 'Reopen Unit' action
  1908. * currently no retries
  1909. *
  1910. * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
  1911. * ZFCP_ERP_SUCCEEDED - action finished successfully
  1912. * ZFCP_ERP_FAILED - action finished unsuccessfully
  1913. */
  1914. static int
  1915. zfcp_erp_unit_strategy(struct zfcp_erp_action *erp_action)
  1916. {
  1917. int retval = ZFCP_ERP_FAILED;
  1918. struct zfcp_unit *unit = erp_action->unit;
  1919. switch (erp_action->step) {
  1920. /*
  1921. * FIXME:
  1922. * the ULP spec. begs for waiting for oustanding commands
  1923. */
  1924. case ZFCP_ERP_STEP_UNINITIALIZED:
  1925. zfcp_erp_unit_strategy_clearstati(unit);
  1926. if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
  1927. retval = zfcp_erp_unit_strategy_close(erp_action);
  1928. break;
  1929. }
  1930. /* else it's already closed, fall through */
  1931. case ZFCP_ERP_STEP_UNIT_CLOSING:
  1932. if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status))
  1933. retval = ZFCP_ERP_FAILED;
  1934. else
  1935. if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
  1936. retval = ZFCP_ERP_EXIT;
  1937. else
  1938. retval =
  1939. zfcp_erp_unit_strategy_open(erp_action);
  1940. break;
  1941. case ZFCP_ERP_STEP_UNIT_OPENING:
  1942. if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status))
  1943. retval = ZFCP_ERP_SUCCEEDED;
  1944. else
  1945. retval = ZFCP_ERP_FAILED;
  1946. break;
  1947. }
  1948. return retval;
  1949. }
  1950. static int
  1951. zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *unit)
  1952. {
  1953. int retval = 0;
  1954. atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING |
  1955. ZFCP_STATUS_COMMON_CLOSING |
  1956. ZFCP_STATUS_COMMON_ACCESS_DENIED |
  1957. ZFCP_STATUS_UNIT_SHARED |
  1958. ZFCP_STATUS_UNIT_READONLY,
  1959. &unit->status);
  1960. return retval;
  1961. }
  1962. /*
  1963. * function:
  1964. *
  1965. * purpose:
  1966. *
  1967. * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
  1968. * ZFCP_ERP_FAILED - action finished unsuccessfully
  1969. */
  1970. static int
  1971. zfcp_erp_unit_strategy_close(struct zfcp_erp_action *erp_action)
  1972. {
  1973. int retval;
  1974. retval = zfcp_fsf_close_unit(erp_action);
  1975. if (retval == -ENOMEM) {
  1976. retval = ZFCP_ERP_NOMEM;
  1977. goto out;
  1978. }
  1979. erp_action->step = ZFCP_ERP_STEP_UNIT_CLOSING;
  1980. if (retval != 0) {
  1981. /* could not send 'close', fail */
  1982. retval = ZFCP_ERP_FAILED;
  1983. goto out;
  1984. }
  1985. retval = ZFCP_ERP_CONTINUES;
  1986. out:
  1987. return retval;
  1988. }
  1989. /*
  1990. * function:
  1991. *
  1992. * purpose:
  1993. *
  1994. * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
  1995. * ZFCP_ERP_FAILED - action finished unsuccessfully
  1996. */
  1997. static int
  1998. zfcp_erp_unit_strategy_open(struct zfcp_erp_action *erp_action)
  1999. {
  2000. int retval;
  2001. retval = zfcp_fsf_open_unit(erp_action);
  2002. if (retval == -ENOMEM) {
  2003. retval = ZFCP_ERP_NOMEM;
  2004. goto out;
  2005. }
  2006. erp_action->step = ZFCP_ERP_STEP_UNIT_OPENING;
  2007. if (retval != 0) {
  2008. /* could not send 'open', fail */
  2009. retval = ZFCP_ERP_FAILED;
  2010. goto out;
  2011. }
  2012. retval = ZFCP_ERP_CONTINUES;
  2013. out:
  2014. return retval;
  2015. }
  2016. void zfcp_erp_start_timer(struct zfcp_fsf_req *fsf_req)
  2017. {
  2018. BUG_ON(!fsf_req->erp_action);
  2019. fsf_req->timer.function = zfcp_erp_timeout_handler;
  2020. fsf_req->timer.data = (unsigned long) fsf_req->erp_action;
  2021. fsf_req->timer.expires = jiffies + ZFCP_ERP_FSFREQ_TIMEOUT;
  2022. add_timer(&fsf_req->timer);
  2023. }
  2024. /*
  2025. * function:
  2026. *
  2027. * purpose: enqueue the specified error recovery action, if needed
  2028. *
  2029. * returns:
  2030. */
  2031. static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
  2032. struct zfcp_port *port,
  2033. struct zfcp_unit *unit, u8 id, void *ref)
  2034. {
  2035. int retval = 1, need = want;
  2036. struct zfcp_erp_action *erp_action = NULL;
  2037. u32 status = 0;
  2038. /*
  2039. * We need some rules here which check whether we really need
  2040. * this action or whether we should just drop it.
  2041. * E.g. if there is a unfinished 'Reopen Port' request then we drop a
  2042. * 'Reopen Unit' request for an associated unit since we can't
  2043. * satisfy this request now. A 'Reopen Port' action will trigger
  2044. * 'Reopen Unit' actions when it completes.
  2045. * Thus, there are only actions in the queue which can immediately be
  2046. * executed. This makes the processing of the action queue more
  2047. * efficient.
  2048. */
  2049. if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
  2050. &adapter->status))
  2051. return -EIO;
  2052. /* check whether we really need this */
  2053. switch (want) {
  2054. case ZFCP_ERP_ACTION_REOPEN_UNIT:
  2055. if (atomic_test_mask
  2056. (ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status)) {
  2057. goto out;
  2058. }
  2059. if (!atomic_test_mask
  2060. (ZFCP_STATUS_COMMON_RUNNING, &port->status) ||
  2061. atomic_test_mask
  2062. (ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
  2063. goto out;
  2064. }
  2065. if (!atomic_test_mask
  2066. (ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
  2067. need = ZFCP_ERP_ACTION_REOPEN_PORT;
  2068. /* fall through !!! */
  2069. case ZFCP_ERP_ACTION_REOPEN_PORT:
  2070. if (atomic_test_mask
  2071. (ZFCP_STATUS_COMMON_ERP_INUSE, &port->status)) {
  2072. goto out;
  2073. }
  2074. /* fall through !!! */
  2075. case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
  2076. if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
  2077. &port->status))
  2078. goto out;
  2079. if (!atomic_test_mask
  2080. (ZFCP_STATUS_COMMON_RUNNING, &adapter->status) ||
  2081. atomic_test_mask
  2082. (ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
  2083. goto out;
  2084. }
  2085. if (!atomic_test_mask
  2086. (ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
  2087. need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
  2088. /* fall through !!! */
  2089. case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
  2090. if (atomic_test_mask
  2091. (ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status)) {
  2092. goto out;
  2093. }
  2094. break;
  2095. default:
  2096. /* unknown erp action */
  2097. goto out;
  2098. }
  2099. /* mark adapter to have some error recovery pending */
  2100. atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
  2101. /* setup error recovery action */
  2102. switch (need) {
  2103. case ZFCP_ERP_ACTION_REOPEN_UNIT:
  2104. zfcp_unit_get(unit);
  2105. atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status);
  2106. erp_action = &unit->erp_action;
  2107. if (!atomic_test_mask
  2108. (ZFCP_STATUS_COMMON_RUNNING, &unit->status))
  2109. status = ZFCP_STATUS_ERP_CLOSE_ONLY;
  2110. break;
  2111. case ZFCP_ERP_ACTION_REOPEN_PORT:
  2112. case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
  2113. zfcp_port_get(port);
  2114. zfcp_erp_action_dismiss_port(port);
  2115. atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
  2116. erp_action = &port->erp_action;
  2117. if (!atomic_test_mask
  2118. (ZFCP_STATUS_COMMON_RUNNING, &port->status))
  2119. status = ZFCP_STATUS_ERP_CLOSE_ONLY;
  2120. break;
  2121. case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
  2122. zfcp_adapter_get(adapter);
  2123. zfcp_erp_action_dismiss_adapter(adapter);
  2124. atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
  2125. erp_action = &adapter->erp_action;
  2126. if (!atomic_test_mask
  2127. (ZFCP_STATUS_COMMON_RUNNING, &adapter->status))
  2128. status = ZFCP_STATUS_ERP_CLOSE_ONLY;
  2129. break;
  2130. }
  2131. memset(erp_action, 0, sizeof (struct zfcp_erp_action));
  2132. erp_action->adapter = adapter;
  2133. erp_action->port = port;
  2134. erp_action->unit = unit;
  2135. erp_action->action = need;
  2136. erp_action->status = status;
  2137. ++adapter->erp_total_count;
  2138. /* finally put it into 'ready' queue and kick erp thread */
  2139. list_add_tail(&erp_action->list, &adapter->erp_ready_head);
  2140. up(&adapter->erp_ready_sem);
  2141. zfcp_rec_dbf_event_thread(1, adapter);
  2142. retval = 0;
  2143. out:
  2144. zfcp_rec_dbf_event_trigger(id, ref, want, need, erp_action,
  2145. adapter, port, unit);
  2146. return retval;
  2147. }
  2148. static int
  2149. zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
  2150. {
  2151. int retval = 0;
  2152. struct zfcp_adapter *adapter = erp_action->adapter;
  2153. --adapter->erp_total_count;
  2154. if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
  2155. --adapter->erp_low_mem_count;
  2156. erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
  2157. }
  2158. list_del(&erp_action->list);
  2159. zfcp_rec_dbf_event_action(144, erp_action);
  2160. switch (erp_action->action) {
  2161. case ZFCP_ERP_ACTION_REOPEN_UNIT:
  2162. atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
  2163. &erp_action->unit->status);
  2164. break;
  2165. case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
  2166. case ZFCP_ERP_ACTION_REOPEN_PORT:
  2167. atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
  2168. &erp_action->port->status);
  2169. break;
  2170. case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
  2171. atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
  2172. &erp_action->adapter->status);
  2173. break;
  2174. default:
  2175. /* bug */
  2176. break;
  2177. }
  2178. return retval;
  2179. }
  2180. /**
  2181. * zfcp_erp_action_cleanup
  2182. *
  2183. * Register unit with scsi stack if appropriate and fix reference counts.
  2184. * Note: Temporary units are not registered with scsi stack.
  2185. */
  2186. static void
  2187. zfcp_erp_action_cleanup(int action, struct zfcp_adapter *adapter,
  2188. struct zfcp_port *port, struct zfcp_unit *unit,
  2189. int result)
  2190. {
  2191. switch (action) {
  2192. case ZFCP_ERP_ACTION_REOPEN_UNIT:
  2193. if ((result == ZFCP_ERP_SUCCEEDED)
  2194. && (!atomic_test_mask(ZFCP_STATUS_UNIT_TEMPORARY,
  2195. &unit->status))
  2196. && !unit->device
  2197. && port->rport) {
  2198. atomic_set_mask(ZFCP_STATUS_UNIT_REGISTERED,
  2199. &unit->status);
  2200. if (atomic_test_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING,
  2201. &unit->status) == 0)
  2202. zfcp_erp_schedule_work(unit);
  2203. }
  2204. zfcp_unit_put(unit);
  2205. break;
  2206. case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
  2207. case ZFCP_ERP_ACTION_REOPEN_PORT:
  2208. if (atomic_test_mask(ZFCP_STATUS_PORT_NO_WWPN,
  2209. &port->status)) {
  2210. zfcp_port_put(port);
  2211. break;
  2212. }
  2213. if ((result == ZFCP_ERP_SUCCEEDED)
  2214. && !port->rport) {
  2215. struct fc_rport_identifiers ids;
  2216. ids.node_name = port->wwnn;
  2217. ids.port_name = port->wwpn;
  2218. ids.port_id = port->d_id;
  2219. ids.roles = FC_RPORT_ROLE_FCP_TARGET;
  2220. port->rport =
  2221. fc_remote_port_add(adapter->scsi_host, 0, &ids);
  2222. if (!port->rport)
  2223. dev_err(&adapter->ccw_device->dev,
  2224. "Failed registration of rport "
  2225. "0x%016Lx.\n", port->wwpn);
  2226. else {
  2227. scsi_target_unblock(&port->rport->dev);
  2228. port->rport->maxframe_size = port->maxframe_size;
  2229. port->rport->supported_classes =
  2230. port->supported_classes;
  2231. }
  2232. }
  2233. if ((result != ZFCP_ERP_SUCCEEDED) && port->rport) {
  2234. fc_remote_port_delete(port->rport);
  2235. port->rport = NULL;
  2236. }
  2237. zfcp_port_put(port);
  2238. break;
  2239. case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
  2240. if (result != ZFCP_ERP_SUCCEEDED) {
  2241. list_for_each_entry(port, &adapter->port_list_head, list)
  2242. if (port->rport &&
  2243. !atomic_test_mask(ZFCP_STATUS_PORT_WKA,
  2244. &port->status)) {
  2245. fc_remote_port_delete(port->rport);
  2246. port->rport = NULL;
  2247. }
  2248. }
  2249. zfcp_adapter_put(adapter);
  2250. break;
  2251. default:
  2252. break;
  2253. }
  2254. }
  2255. static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
  2256. {
  2257. struct zfcp_port *port;
  2258. if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status))
  2259. zfcp_erp_action_dismiss(&adapter->erp_action);
  2260. else
  2261. list_for_each_entry(port, &adapter->port_list_head, list)
  2262. zfcp_erp_action_dismiss_port(port);
  2263. }
  2264. static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
  2265. {
  2266. struct zfcp_unit *unit;
  2267. if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status))
  2268. zfcp_erp_action_dismiss(&port->erp_action);
  2269. else
  2270. list_for_each_entry(unit, &port->unit_list_head, list)
  2271. zfcp_erp_action_dismiss_unit(unit);
  2272. }
  2273. static void zfcp_erp_action_dismiss_unit(struct zfcp_unit *unit)
  2274. {
  2275. if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status))
  2276. zfcp_erp_action_dismiss(&unit->erp_action);
  2277. }
  2278. static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
  2279. {
  2280. list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
  2281. zfcp_rec_dbf_event_action(145, erp_action);
  2282. }
  2283. static void zfcp_erp_action_to_ready(struct zfcp_erp_action *erp_action)
  2284. {
  2285. list_move(&erp_action->list, &erp_action->adapter->erp_ready_head);
  2286. zfcp_rec_dbf_event_action(146, erp_action);
  2287. }
  2288. void zfcp_erp_port_boxed(struct zfcp_port *port, u8 id, void *ref)
  2289. {
  2290. unsigned long flags;
  2291. read_lock_irqsave(&zfcp_data.config_lock, flags);
  2292. zfcp_erp_modify_port_status(port, id, ref,
  2293. ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
  2294. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  2295. zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
  2296. }
  2297. void zfcp_erp_unit_boxed(struct zfcp_unit *unit, u8 id, void *ref)
  2298. {
  2299. zfcp_erp_modify_unit_status(unit, id, ref,
  2300. ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
  2301. zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
  2302. }
  2303. void zfcp_erp_port_access_denied(struct zfcp_port *port, u8 id, void *ref)
  2304. {
  2305. unsigned long flags;
  2306. read_lock_irqsave(&zfcp_data.config_lock, flags);
  2307. zfcp_erp_modify_port_status(port, id, ref,
  2308. ZFCP_STATUS_COMMON_ERP_FAILED |
  2309. ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
  2310. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  2311. }
  2312. void zfcp_erp_unit_access_denied(struct zfcp_unit *unit, u8 id, void *ref)
  2313. {
  2314. zfcp_erp_modify_unit_status(unit, id, ref,
  2315. ZFCP_STATUS_COMMON_ERP_FAILED |
  2316. ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
  2317. }
  2318. void zfcp_erp_adapter_access_changed(struct zfcp_adapter *adapter, u8 id,
  2319. void *ref)
  2320. {
  2321. struct zfcp_port *port;
  2322. unsigned long flags;
  2323. if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
  2324. return;
  2325. read_lock_irqsave(&zfcp_data.config_lock, flags);
  2326. if (adapter->nameserver_port)
  2327. zfcp_erp_port_access_changed(adapter->nameserver_port, id, ref);
  2328. list_for_each_entry(port, &adapter->port_list_head, list)
  2329. if (port != adapter->nameserver_port)
  2330. zfcp_erp_port_access_changed(port, id, ref);
  2331. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  2332. }
  2333. void zfcp_erp_port_access_changed(struct zfcp_port *port, u8 id, void *ref)
  2334. {
  2335. struct zfcp_unit *unit;
  2336. if (!atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED,
  2337. &port->status) &&
  2338. !atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_BOXED,
  2339. &port->status)) {
  2340. if (!atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
  2341. list_for_each_entry(unit, &port->unit_list_head, list)
  2342. zfcp_erp_unit_access_changed(unit, id, ref);
  2343. return;
  2344. }
  2345. zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
  2346. }
  2347. void zfcp_erp_unit_access_changed(struct zfcp_unit *unit, u8 id, void *ref)
  2348. {
  2349. if (!atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED,
  2350. &unit->status) &&
  2351. !atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_BOXED,
  2352. &unit->status))
  2353. return;
  2354. zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
  2355. }