libiscsi.c 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474
  1. /*
  2. * iSCSI lib functions
  3. *
  4. * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  5. * Copyright (C) 2004 - 2006 Mike Christie
  6. * Copyright (C) 2004 - 2005 Dmitry Yusupov
  7. * Copyright (C) 2004 - 2005 Alex Aizman
  8. * maintained by open-iscsi@googlegroups.com
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. #include <linux/types.h>
  25. #include <linux/kfifo.h>
  26. #include <linux/delay.h>
  27. #include <linux/log2.h>
  28. #include <linux/slab.h>
  29. #include <asm/unaligned.h>
  30. #include <net/tcp.h>
  31. #include <scsi/scsi_cmnd.h>
  32. #include <scsi/scsi_device.h>
  33. #include <scsi/scsi_eh.h>
  34. #include <scsi/scsi_tcq.h>
  35. #include <scsi/scsi_host.h>
  36. #include <scsi/scsi.h>
  37. #include <scsi/iscsi_proto.h>
  38. #include <scsi/scsi_transport.h>
  39. #include <scsi/scsi_transport_iscsi.h>
  40. #include <scsi/libiscsi.h>
  41. static int iscsi_dbg_lib_conn;
  42. module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
  43. S_IRUGO | S_IWUSR);
  44. MODULE_PARM_DESC(debug_libiscsi_conn,
  45. "Turn on debugging for connections in libiscsi module. "
  46. "Set to 1 to turn on, and zero to turn off. Default is off.");
  47. static int iscsi_dbg_lib_session;
  48. module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
  49. S_IRUGO | S_IWUSR);
  50. MODULE_PARM_DESC(debug_libiscsi_session,
  51. "Turn on debugging for sessions in libiscsi module. "
  52. "Set to 1 to turn on, and zero to turn off. Default is off.");
  53. static int iscsi_dbg_lib_eh;
  54. module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
  55. S_IRUGO | S_IWUSR);
  56. MODULE_PARM_DESC(debug_libiscsi_eh,
  57. "Turn on debugging for error handling in libiscsi module. "
  58. "Set to 1 to turn on, and zero to turn off. Default is off.");
  59. #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
  60. do { \
  61. if (iscsi_dbg_lib_conn) \
  62. iscsi_conn_printk(KERN_INFO, _conn, \
  63. "%s " dbg_fmt, \
  64. __func__, ##arg); \
  65. } while (0);
  66. #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
  67. do { \
  68. if (iscsi_dbg_lib_session) \
  69. iscsi_session_printk(KERN_INFO, _session, \
  70. "%s " dbg_fmt, \
  71. __func__, ##arg); \
  72. } while (0);
  73. #define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
  74. do { \
  75. if (iscsi_dbg_lib_eh) \
  76. iscsi_session_printk(KERN_INFO, _session, \
  77. "%s " dbg_fmt, \
  78. __func__, ##arg); \
  79. } while (0);
  80. inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
  81. {
  82. struct Scsi_Host *shost = conn->session->host;
  83. struct iscsi_host *ihost = shost_priv(shost);
  84. if (ihost->workq)
  85. queue_work(ihost->workq, &conn->xmitwork);
  86. }
  87. EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
  88. static void __iscsi_update_cmdsn(struct iscsi_session *session,
  89. uint32_t exp_cmdsn, uint32_t max_cmdsn)
  90. {
  91. /*
  92. * standard specifies this check for when to update expected and
  93. * max sequence numbers
  94. */
  95. if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
  96. return;
  97. if (exp_cmdsn != session->exp_cmdsn &&
  98. !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
  99. session->exp_cmdsn = exp_cmdsn;
  100. if (max_cmdsn != session->max_cmdsn &&
  101. !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
  102. session->max_cmdsn = max_cmdsn;
  103. /*
  104. * if the window closed with IO queued, then kick the
  105. * xmit thread
  106. */
  107. if (!list_empty(&session->leadconn->cmdqueue) ||
  108. !list_empty(&session->leadconn->mgmtqueue))
  109. iscsi_conn_queue_work(session->leadconn);
  110. }
  111. }
  112. void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
  113. {
  114. __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
  115. be32_to_cpu(hdr->max_cmdsn));
  116. }
  117. EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
  118. /**
  119. * iscsi_prep_data_out_pdu - initialize Data-Out
  120. * @task: scsi command task
  121. * @r2t: R2T info
  122. * @hdr: iscsi data in pdu
  123. *
  124. * Notes:
  125. * Initialize Data-Out within this R2T sequence and finds
  126. * proper data_offset within this SCSI command.
  127. *
  128. * This function is called with connection lock taken.
  129. **/
  130. void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
  131. struct iscsi_data *hdr)
  132. {
  133. struct iscsi_conn *conn = task->conn;
  134. unsigned int left = r2t->data_length - r2t->sent;
  135. task->hdr_len = sizeof(struct iscsi_data);
  136. memset(hdr, 0, sizeof(struct iscsi_data));
  137. hdr->ttt = r2t->ttt;
  138. hdr->datasn = cpu_to_be32(r2t->datasn);
  139. r2t->datasn++;
  140. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  141. hdr->lun = task->lun;
  142. hdr->itt = task->hdr_itt;
  143. hdr->exp_statsn = r2t->exp_statsn;
  144. hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
  145. if (left > conn->max_xmit_dlength) {
  146. hton24(hdr->dlength, conn->max_xmit_dlength);
  147. r2t->data_count = conn->max_xmit_dlength;
  148. hdr->flags = 0;
  149. } else {
  150. hton24(hdr->dlength, left);
  151. r2t->data_count = left;
  152. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  153. }
  154. conn->dataout_pdus_cnt++;
  155. }
  156. EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
  157. static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
  158. {
  159. unsigned exp_len = task->hdr_len + len;
  160. if (exp_len > task->hdr_max) {
  161. WARN_ON(1);
  162. return -EINVAL;
  163. }
  164. WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
  165. task->hdr_len = exp_len;
  166. return 0;
  167. }
  168. /*
  169. * make an extended cdb AHS
  170. */
  171. static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
  172. {
  173. struct scsi_cmnd *cmd = task->sc;
  174. unsigned rlen, pad_len;
  175. unsigned short ahslength;
  176. struct iscsi_ecdb_ahdr *ecdb_ahdr;
  177. int rc;
  178. ecdb_ahdr = iscsi_next_hdr(task);
  179. rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
  180. BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
  181. ahslength = rlen + sizeof(ecdb_ahdr->reserved);
  182. pad_len = iscsi_padding(rlen);
  183. rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
  184. sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
  185. if (rc)
  186. return rc;
  187. if (pad_len)
  188. memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
  189. ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
  190. ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
  191. ecdb_ahdr->reserved = 0;
  192. memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
  193. ISCSI_DBG_SESSION(task->conn->session,
  194. "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
  195. "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
  196. "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
  197. task->hdr_len);
  198. return 0;
  199. }
  200. static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
  201. {
  202. struct scsi_cmnd *sc = task->sc;
  203. struct iscsi_rlength_ahdr *rlen_ahdr;
  204. int rc;
  205. rlen_ahdr = iscsi_next_hdr(task);
  206. rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
  207. if (rc)
  208. return rc;
  209. rlen_ahdr->ahslength =
  210. cpu_to_be16(sizeof(rlen_ahdr->read_length) +
  211. sizeof(rlen_ahdr->reserved));
  212. rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
  213. rlen_ahdr->reserved = 0;
  214. rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
  215. ISCSI_DBG_SESSION(task->conn->session,
  216. "bidi-in rlen_ahdr->read_length(%d) "
  217. "rlen_ahdr->ahslength(%d)\n",
  218. be32_to_cpu(rlen_ahdr->read_length),
  219. be16_to_cpu(rlen_ahdr->ahslength));
  220. return 0;
  221. }
  222. /**
  223. * iscsi_check_tmf_restrictions - check if a task is affected by TMF
  224. * @task: iscsi task
  225. * @opcode: opcode to check for
  226. *
  227. * During TMF a task has to be checked if it's affected.
  228. * All unrelated I/O can be passed through, but I/O to the
  229. * affected LUN should be restricted.
  230. * If 'fast_abort' is set we won't be sending any I/O to the
  231. * affected LUN.
  232. * Otherwise the target is waiting for all TTTs to be completed,
  233. * so we have to send all outstanding Data-Out PDUs to the target.
  234. */
  235. static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode)
  236. {
  237. struct iscsi_conn *conn = task->conn;
  238. struct iscsi_tm *tmf = &conn->tmhdr;
  239. unsigned int hdr_lun;
  240. if (conn->tmf_state == TMF_INITIAL)
  241. return 0;
  242. if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC)
  243. return 0;
  244. switch (ISCSI_TM_FUNC_VALUE(tmf)) {
  245. case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
  246. /*
  247. * Allow PDUs for unrelated LUNs
  248. */
  249. hdr_lun = scsilun_to_int(&tmf->lun);
  250. if (hdr_lun != task->sc->device->lun)
  251. return 0;
  252. /* fall through */
  253. case ISCSI_TM_FUNC_TARGET_WARM_RESET:
  254. /*
  255. * Fail all SCSI cmd PDUs
  256. */
  257. if (opcode != ISCSI_OP_SCSI_DATA_OUT) {
  258. iscsi_conn_printk(KERN_INFO, conn,
  259. "task [op %x/%x itt "
  260. "0x%x/0x%x] "
  261. "rejected.\n",
  262. task->hdr->opcode, opcode,
  263. task->itt, task->hdr_itt);
  264. return -EACCES;
  265. }
  266. /*
  267. * And also all data-out PDUs in response to R2T
  268. * if fast_abort is set.
  269. */
  270. if (conn->session->fast_abort) {
  271. iscsi_conn_printk(KERN_INFO, conn,
  272. "task [op %x/%x itt "
  273. "0x%x/0x%x] fast abort.\n",
  274. task->hdr->opcode, opcode,
  275. task->itt, task->hdr_itt);
  276. return -EACCES;
  277. }
  278. break;
  279. case ISCSI_TM_FUNC_ABORT_TASK:
  280. /*
  281. * the caller has already checked if the task
  282. * they want to abort was in the pending queue so if
  283. * we are here the cmd pdu has gone out already, and
  284. * we will only hit this for data-outs
  285. */
  286. if (opcode == ISCSI_OP_SCSI_DATA_OUT &&
  287. task->hdr_itt == tmf->rtt) {
  288. ISCSI_DBG_SESSION(conn->session,
  289. "Preventing task %x/%x from sending "
  290. "data-out due to abort task in "
  291. "progress\n", task->itt,
  292. task->hdr_itt);
  293. return -EACCES;
  294. }
  295. break;
  296. }
  297. return 0;
  298. }
  299. /**
  300. * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
  301. * @task: iscsi task
  302. *
  303. * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
  304. * fields like dlength or final based on how much data it sends
  305. */
  306. static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
  307. {
  308. struct iscsi_conn *conn = task->conn;
  309. struct iscsi_session *session = conn->session;
  310. struct scsi_cmnd *sc = task->sc;
  311. struct iscsi_scsi_req *hdr;
  312. unsigned hdrlength, cmd_len;
  313. itt_t itt;
  314. int rc;
  315. rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD);
  316. if (rc)
  317. return rc;
  318. if (conn->session->tt->alloc_pdu) {
  319. rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
  320. if (rc)
  321. return rc;
  322. }
  323. hdr = (struct iscsi_scsi_req *)task->hdr;
  324. itt = hdr->itt;
  325. memset(hdr, 0, sizeof(*hdr));
  326. if (session->tt->parse_pdu_itt)
  327. hdr->itt = task->hdr_itt = itt;
  328. else
  329. hdr->itt = task->hdr_itt = build_itt(task->itt,
  330. task->conn->session->age);
  331. task->hdr_len = 0;
  332. rc = iscsi_add_hdr(task, sizeof(*hdr));
  333. if (rc)
  334. return rc;
  335. hdr->opcode = ISCSI_OP_SCSI_CMD;
  336. hdr->flags = ISCSI_ATTR_SIMPLE;
  337. int_to_scsilun(sc->device->lun, &hdr->lun);
  338. task->lun = hdr->lun;
  339. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  340. cmd_len = sc->cmd_len;
  341. if (cmd_len < ISCSI_CDB_SIZE)
  342. memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
  343. else if (cmd_len > ISCSI_CDB_SIZE) {
  344. rc = iscsi_prep_ecdb_ahs(task);
  345. if (rc)
  346. return rc;
  347. cmd_len = ISCSI_CDB_SIZE;
  348. }
  349. memcpy(hdr->cdb, sc->cmnd, cmd_len);
  350. task->imm_count = 0;
  351. if (scsi_bidi_cmnd(sc)) {
  352. hdr->flags |= ISCSI_FLAG_CMD_READ;
  353. rc = iscsi_prep_bidi_ahs(task);
  354. if (rc)
  355. return rc;
  356. }
  357. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  358. unsigned out_len = scsi_out(sc)->length;
  359. struct iscsi_r2t_info *r2t = &task->unsol_r2t;
  360. hdr->data_length = cpu_to_be32(out_len);
  361. hdr->flags |= ISCSI_FLAG_CMD_WRITE;
  362. /*
  363. * Write counters:
  364. *
  365. * imm_count bytes to be sent right after
  366. * SCSI PDU Header
  367. *
  368. * unsol_count bytes(as Data-Out) to be sent
  369. * without R2T ack right after
  370. * immediate data
  371. *
  372. * r2t data_length bytes to be sent via R2T ack's
  373. *
  374. * pad_count bytes to be sent as zero-padding
  375. */
  376. memset(r2t, 0, sizeof(*r2t));
  377. if (session->imm_data_en) {
  378. if (out_len >= session->first_burst)
  379. task->imm_count = min(session->first_burst,
  380. conn->max_xmit_dlength);
  381. else
  382. task->imm_count = min(out_len,
  383. conn->max_xmit_dlength);
  384. hton24(hdr->dlength, task->imm_count);
  385. } else
  386. zero_data(hdr->dlength);
  387. if (!session->initial_r2t_en) {
  388. r2t->data_length = min(session->first_burst, out_len) -
  389. task->imm_count;
  390. r2t->data_offset = task->imm_count;
  391. r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
  392. r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
  393. }
  394. if (!task->unsol_r2t.data_length)
  395. /* No unsolicit Data-Out's */
  396. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  397. } else {
  398. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  399. zero_data(hdr->dlength);
  400. hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
  401. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  402. hdr->flags |= ISCSI_FLAG_CMD_READ;
  403. }
  404. /* calculate size of additional header segments (AHSs) */
  405. hdrlength = task->hdr_len - sizeof(*hdr);
  406. WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
  407. hdrlength /= ISCSI_PAD_LEN;
  408. WARN_ON(hdrlength >= 256);
  409. hdr->hlength = hdrlength & 0xFF;
  410. hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
  411. if (session->tt->init_task && session->tt->init_task(task))
  412. return -EIO;
  413. task->state = ISCSI_TASK_RUNNING;
  414. session->cmdsn++;
  415. conn->scsicmd_pdus_cnt++;
  416. ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
  417. "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
  418. scsi_bidi_cmnd(sc) ? "bidirectional" :
  419. sc->sc_data_direction == DMA_TO_DEVICE ?
  420. "write" : "read", conn->id, sc, sc->cmnd[0],
  421. task->itt, scsi_bufflen(sc),
  422. scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
  423. session->cmdsn,
  424. session->max_cmdsn - session->exp_cmdsn + 1);
  425. return 0;
  426. }
  427. /**
  428. * iscsi_free_task - free a task
  429. * @task: iscsi cmd task
  430. *
  431. * Must be called with session lock.
  432. * This function returns the scsi command to scsi-ml or cleans
  433. * up mgmt tasks then returns the task to the pool.
  434. */
  435. static void iscsi_free_task(struct iscsi_task *task)
  436. {
  437. struct iscsi_conn *conn = task->conn;
  438. struct iscsi_session *session = conn->session;
  439. struct scsi_cmnd *sc = task->sc;
  440. int oldstate = task->state;
  441. ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
  442. task->itt, task->state, task->sc);
  443. session->tt->cleanup_task(task);
  444. task->state = ISCSI_TASK_FREE;
  445. task->sc = NULL;
  446. /*
  447. * login task is preallocated so do not free
  448. */
  449. if (conn->login_task == task)
  450. return;
  451. kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
  452. if (sc) {
  453. task->sc = NULL;
  454. /* SCSI eh reuses commands to verify us */
  455. sc->SCp.ptr = NULL;
  456. /*
  457. * queue command may call this to free the task, so
  458. * it will decide how to return sc to scsi-ml.
  459. */
  460. if (oldstate != ISCSI_TASK_REQUEUE_SCSIQ)
  461. sc->scsi_done(sc);
  462. }
  463. }
  464. void __iscsi_get_task(struct iscsi_task *task)
  465. {
  466. atomic_inc(&task->refcount);
  467. }
  468. EXPORT_SYMBOL_GPL(__iscsi_get_task);
  469. void __iscsi_put_task(struct iscsi_task *task)
  470. {
  471. if (atomic_dec_and_test(&task->refcount))
  472. iscsi_free_task(task);
  473. }
  474. EXPORT_SYMBOL_GPL(__iscsi_put_task);
  475. void iscsi_put_task(struct iscsi_task *task)
  476. {
  477. struct iscsi_session *session = task->conn->session;
  478. spin_lock_bh(&session->lock);
  479. __iscsi_put_task(task);
  480. spin_unlock_bh(&session->lock);
  481. }
  482. EXPORT_SYMBOL_GPL(iscsi_put_task);
  483. /**
  484. * iscsi_complete_task - finish a task
  485. * @task: iscsi cmd task
  486. * @state: state to complete task with
  487. *
  488. * Must be called with session lock.
  489. */
  490. static void iscsi_complete_task(struct iscsi_task *task, int state)
  491. {
  492. struct iscsi_conn *conn = task->conn;
  493. ISCSI_DBG_SESSION(conn->session,
  494. "complete task itt 0x%x state %d sc %p\n",
  495. task->itt, task->state, task->sc);
  496. if (task->state == ISCSI_TASK_COMPLETED ||
  497. task->state == ISCSI_TASK_ABRT_TMF ||
  498. task->state == ISCSI_TASK_ABRT_SESS_RECOV ||
  499. task->state == ISCSI_TASK_REQUEUE_SCSIQ)
  500. return;
  501. WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
  502. task->state = state;
  503. if (!list_empty(&task->running))
  504. list_del_init(&task->running);
  505. if (conn->task == task)
  506. conn->task = NULL;
  507. if (conn->ping_task == task)
  508. conn->ping_task = NULL;
  509. /* release get from queueing */
  510. __iscsi_put_task(task);
  511. }
  512. /**
  513. * iscsi_complete_scsi_task - finish scsi task normally
  514. * @task: iscsi task for scsi cmd
  515. * @exp_cmdsn: expected cmd sn in cpu format
  516. * @max_cmdsn: max cmd sn in cpu format
  517. *
  518. * This is used when drivers do not need or cannot perform
  519. * lower level pdu processing.
  520. *
  521. * Called with session lock
  522. */
  523. void iscsi_complete_scsi_task(struct iscsi_task *task,
  524. uint32_t exp_cmdsn, uint32_t max_cmdsn)
  525. {
  526. struct iscsi_conn *conn = task->conn;
  527. ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
  528. conn->last_recv = jiffies;
  529. __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
  530. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  531. }
  532. EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
  533. /*
  534. * session lock must be held and if not called for a task that is
  535. * still pending or from the xmit thread, then xmit thread must
  536. * be suspended.
  537. */
  538. static void fail_scsi_task(struct iscsi_task *task, int err)
  539. {
  540. struct iscsi_conn *conn = task->conn;
  541. struct scsi_cmnd *sc;
  542. int state;
  543. /*
  544. * if a command completes and we get a successful tmf response
  545. * we will hit this because the scsi eh abort code does not take
  546. * a ref to the task.
  547. */
  548. sc = task->sc;
  549. if (!sc)
  550. return;
  551. if (task->state == ISCSI_TASK_PENDING) {
  552. /*
  553. * cmd never made it to the xmit thread, so we should not count
  554. * the cmd in the sequencing
  555. */
  556. conn->session->queued_cmdsn--;
  557. /* it was never sent so just complete like normal */
  558. state = ISCSI_TASK_COMPLETED;
  559. } else if (err == DID_TRANSPORT_DISRUPTED)
  560. state = ISCSI_TASK_ABRT_SESS_RECOV;
  561. else
  562. state = ISCSI_TASK_ABRT_TMF;
  563. sc->result = err << 16;
  564. if (!scsi_bidi_cmnd(sc))
  565. scsi_set_resid(sc, scsi_bufflen(sc));
  566. else {
  567. scsi_out(sc)->resid = scsi_out(sc)->length;
  568. scsi_in(sc)->resid = scsi_in(sc)->length;
  569. }
  570. iscsi_complete_task(task, state);
  571. }
  572. static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
  573. struct iscsi_task *task)
  574. {
  575. struct iscsi_session *session = conn->session;
  576. struct iscsi_hdr *hdr = task->hdr;
  577. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  578. uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  579. if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
  580. return -ENOTCONN;
  581. if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
  582. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  583. /*
  584. * pre-format CmdSN for outgoing PDU.
  585. */
  586. nop->cmdsn = cpu_to_be32(session->cmdsn);
  587. if (hdr->itt != RESERVED_ITT) {
  588. /*
  589. * TODO: We always use immediate for normal session pdus.
  590. * If we start to send tmfs or nops as non-immediate then
  591. * we should start checking the cmdsn numbers for mgmt tasks.
  592. *
  593. * During discovery sessions iscsid sends TEXT as non immediate,
  594. * but we always only send one PDU at a time.
  595. */
  596. if (conn->c_stage == ISCSI_CONN_STARTED &&
  597. !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
  598. session->queued_cmdsn++;
  599. session->cmdsn++;
  600. }
  601. }
  602. if (session->tt->init_task && session->tt->init_task(task))
  603. return -EIO;
  604. if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
  605. session->state = ISCSI_STATE_LOGGING_OUT;
  606. task->state = ISCSI_TASK_RUNNING;
  607. ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
  608. "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
  609. hdr->itt, task->data_count);
  610. return 0;
  611. }
  612. static struct iscsi_task *
  613. __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  614. char *data, uint32_t data_size)
  615. {
  616. struct iscsi_session *session = conn->session;
  617. struct iscsi_host *ihost = shost_priv(session->host);
  618. uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  619. struct iscsi_task *task;
  620. itt_t itt;
  621. if (session->state == ISCSI_STATE_TERMINATE)
  622. return NULL;
  623. if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
  624. /*
  625. * Login and Text are sent serially, in
  626. * request-followed-by-response sequence.
  627. * Same task can be used. Same ITT must be used.
  628. * Note that login_task is preallocated at conn_create().
  629. */
  630. if (conn->login_task->state != ISCSI_TASK_FREE) {
  631. iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
  632. "progress. Cannot start new task.\n");
  633. return NULL;
  634. }
  635. task = conn->login_task;
  636. } else {
  637. if (session->state != ISCSI_STATE_LOGGED_IN)
  638. return NULL;
  639. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  640. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  641. if (!kfifo_out(&session->cmdpool.queue,
  642. (void*)&task, sizeof(void*)))
  643. return NULL;
  644. }
  645. /*
  646. * released in complete pdu for task we expect a response for, and
  647. * released by the lld when it has transmitted the task for
  648. * pdus we do not expect a response for.
  649. */
  650. atomic_set(&task->refcount, 1);
  651. task->conn = conn;
  652. task->sc = NULL;
  653. INIT_LIST_HEAD(&task->running);
  654. task->state = ISCSI_TASK_PENDING;
  655. if (data_size) {
  656. memcpy(task->data, data, data_size);
  657. task->data_count = data_size;
  658. } else
  659. task->data_count = 0;
  660. if (conn->session->tt->alloc_pdu) {
  661. if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
  662. iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
  663. "pdu for mgmt task.\n");
  664. goto free_task;
  665. }
  666. }
  667. itt = task->hdr->itt;
  668. task->hdr_len = sizeof(struct iscsi_hdr);
  669. memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
  670. if (hdr->itt != RESERVED_ITT) {
  671. if (session->tt->parse_pdu_itt)
  672. task->hdr->itt = itt;
  673. else
  674. task->hdr->itt = build_itt(task->itt,
  675. task->conn->session->age);
  676. }
  677. if (!ihost->workq) {
  678. if (iscsi_prep_mgmt_task(conn, task))
  679. goto free_task;
  680. if (session->tt->xmit_task(task))
  681. goto free_task;
  682. } else {
  683. list_add_tail(&task->running, &conn->mgmtqueue);
  684. iscsi_conn_queue_work(conn);
  685. }
  686. return task;
  687. free_task:
  688. __iscsi_put_task(task);
  689. return NULL;
  690. }
  691. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  692. char *data, uint32_t data_size)
  693. {
  694. struct iscsi_conn *conn = cls_conn->dd_data;
  695. struct iscsi_session *session = conn->session;
  696. int err = 0;
  697. spin_lock_bh(&session->lock);
  698. if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
  699. err = -EPERM;
  700. spin_unlock_bh(&session->lock);
  701. return err;
  702. }
  703. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  704. /**
  705. * iscsi_cmd_rsp - SCSI Command Response processing
  706. * @conn: iscsi connection
  707. * @hdr: iscsi header
  708. * @task: scsi command task
  709. * @data: cmd data buffer
  710. * @datalen: len of buffer
  711. *
  712. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  713. * then completes the command and task.
  714. **/
  715. static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  716. struct iscsi_task *task, char *data,
  717. int datalen)
  718. {
  719. struct iscsi_scsi_rsp *rhdr = (struct iscsi_scsi_rsp *)hdr;
  720. struct iscsi_session *session = conn->session;
  721. struct scsi_cmnd *sc = task->sc;
  722. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  723. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  724. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  725. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  726. sc->result = DID_ERROR << 16;
  727. goto out;
  728. }
  729. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  730. uint16_t senselen;
  731. if (datalen < 2) {
  732. invalid_datalen:
  733. iscsi_conn_printk(KERN_ERR, conn,
  734. "Got CHECK_CONDITION but invalid data "
  735. "buffer size of %d\n", datalen);
  736. sc->result = DID_BAD_TARGET << 16;
  737. goto out;
  738. }
  739. senselen = get_unaligned_be16(data);
  740. if (datalen < senselen)
  741. goto invalid_datalen;
  742. memcpy(sc->sense_buffer, data + 2,
  743. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  744. ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
  745. min_t(uint16_t, senselen,
  746. SCSI_SENSE_BUFFERSIZE));
  747. }
  748. if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
  749. ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
  750. int res_count = be32_to_cpu(rhdr->bi_residual_count);
  751. if (scsi_bidi_cmnd(sc) && res_count > 0 &&
  752. (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
  753. res_count <= scsi_in(sc)->length))
  754. scsi_in(sc)->resid = res_count;
  755. else
  756. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  757. }
  758. if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
  759. ISCSI_FLAG_CMD_OVERFLOW)) {
  760. int res_count = be32_to_cpu(rhdr->residual_count);
  761. if (res_count > 0 &&
  762. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  763. res_count <= scsi_bufflen(sc)))
  764. /* write side for bidi or uni-io set_resid */
  765. scsi_set_resid(sc, res_count);
  766. else
  767. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  768. }
  769. out:
  770. ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
  771. sc, sc->result, task->itt);
  772. conn->scsirsp_pdus_cnt++;
  773. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  774. }
  775. /**
  776. * iscsi_data_in_rsp - SCSI Data-In Response processing
  777. * @conn: iscsi connection
  778. * @hdr: iscsi pdu
  779. * @task: scsi command task
  780. **/
  781. static void
  782. iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  783. struct iscsi_task *task)
  784. {
  785. struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
  786. struct scsi_cmnd *sc = task->sc;
  787. if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
  788. return;
  789. iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
  790. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  791. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  792. if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
  793. ISCSI_FLAG_DATA_OVERFLOW)) {
  794. int res_count = be32_to_cpu(rhdr->residual_count);
  795. if (res_count > 0 &&
  796. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  797. res_count <= scsi_in(sc)->length))
  798. scsi_in(sc)->resid = res_count;
  799. else
  800. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  801. }
  802. ISCSI_DBG_SESSION(conn->session, "data in with status done "
  803. "[sc %p res %d itt 0x%x]\n",
  804. sc, sc->result, task->itt);
  805. conn->scsirsp_pdus_cnt++;
  806. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  807. }
  808. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  809. {
  810. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  811. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  812. conn->tmfrsp_pdus_cnt++;
  813. if (conn->tmf_state != TMF_QUEUED)
  814. return;
  815. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  816. conn->tmf_state = TMF_SUCCESS;
  817. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  818. conn->tmf_state = TMF_NOT_FOUND;
  819. else
  820. conn->tmf_state = TMF_FAILED;
  821. wake_up(&conn->ehwait);
  822. }
  823. static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
  824. {
  825. struct iscsi_nopout hdr;
  826. struct iscsi_task *task;
  827. if (!rhdr && conn->ping_task)
  828. return;
  829. memset(&hdr, 0, sizeof(struct iscsi_nopout));
  830. hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
  831. hdr.flags = ISCSI_FLAG_CMD_FINAL;
  832. if (rhdr) {
  833. hdr.lun = rhdr->lun;
  834. hdr.ttt = rhdr->ttt;
  835. hdr.itt = RESERVED_ITT;
  836. } else
  837. hdr.ttt = RESERVED_ITT;
  838. task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
  839. if (!task)
  840. iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
  841. else if (!rhdr) {
  842. /* only track our nops */
  843. conn->ping_task = task;
  844. conn->last_ping = jiffies;
  845. }
  846. }
  847. static int iscsi_nop_out_rsp(struct iscsi_task *task,
  848. struct iscsi_nopin *nop, char *data, int datalen)
  849. {
  850. struct iscsi_conn *conn = task->conn;
  851. int rc = 0;
  852. if (conn->ping_task != task) {
  853. /*
  854. * If this is not in response to one of our
  855. * nops then it must be from userspace.
  856. */
  857. if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
  858. data, datalen))
  859. rc = ISCSI_ERR_CONN_FAILED;
  860. } else
  861. mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
  862. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  863. return rc;
  864. }
  865. static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  866. char *data, int datalen)
  867. {
  868. struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
  869. struct iscsi_hdr rejected_pdu;
  870. int opcode, rc = 0;
  871. conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
  872. if (ntoh24(reject->dlength) > datalen ||
  873. ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
  874. iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
  875. "pdu. Invalid data length (pdu dlength "
  876. "%u, datalen %d\n", ntoh24(reject->dlength),
  877. datalen);
  878. return ISCSI_ERR_PROTO;
  879. }
  880. memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
  881. opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
  882. switch (reject->reason) {
  883. case ISCSI_REASON_DATA_DIGEST_ERROR:
  884. iscsi_conn_printk(KERN_ERR, conn,
  885. "pdu (op 0x%x itt 0x%x) rejected "
  886. "due to DataDigest error.\n",
  887. rejected_pdu.itt, opcode);
  888. break;
  889. case ISCSI_REASON_IMM_CMD_REJECT:
  890. iscsi_conn_printk(KERN_ERR, conn,
  891. "pdu (op 0x%x itt 0x%x) rejected. Too many "
  892. "immediate commands.\n",
  893. rejected_pdu.itt, opcode);
  894. /*
  895. * We only send one TMF at a time so if the target could not
  896. * handle it, then it should get fixed (RFC mandates that
  897. * a target can handle one immediate TMF per conn).
  898. *
  899. * For nops-outs, we could have sent more than one if
  900. * the target is sending us lots of nop-ins
  901. */
  902. if (opcode != ISCSI_OP_NOOP_OUT)
  903. return 0;
  904. if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG))
  905. /*
  906. * nop-out in response to target's nop-out rejected.
  907. * Just resend.
  908. */
  909. iscsi_send_nopout(conn,
  910. (struct iscsi_nopin*)&rejected_pdu);
  911. else {
  912. struct iscsi_task *task;
  913. /*
  914. * Our nop as ping got dropped. We know the target
  915. * and transport are ok so just clean up
  916. */
  917. task = iscsi_itt_to_task(conn, rejected_pdu.itt);
  918. if (!task) {
  919. iscsi_conn_printk(KERN_ERR, conn,
  920. "Invalid pdu reject. Could "
  921. "not lookup rejected task.\n");
  922. rc = ISCSI_ERR_BAD_ITT;
  923. } else
  924. rc = iscsi_nop_out_rsp(task,
  925. (struct iscsi_nopin*)&rejected_pdu,
  926. NULL, 0);
  927. }
  928. break;
  929. default:
  930. iscsi_conn_printk(KERN_ERR, conn,
  931. "pdu (op 0x%x itt 0x%x) rejected. Reason "
  932. "code 0x%x\n", rejected_pdu.itt,
  933. rejected_pdu.opcode, reject->reason);
  934. break;
  935. }
  936. return rc;
  937. }
  938. /**
  939. * iscsi_itt_to_task - look up task by itt
  940. * @conn: iscsi connection
  941. * @itt: itt
  942. *
  943. * This should be used for mgmt tasks like login and nops, or if
  944. * the LDD's itt space does not include the session age.
  945. *
  946. * The session lock must be held.
  947. */
  948. struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
  949. {
  950. struct iscsi_session *session = conn->session;
  951. int i;
  952. if (itt == RESERVED_ITT)
  953. return NULL;
  954. if (session->tt->parse_pdu_itt)
  955. session->tt->parse_pdu_itt(conn, itt, &i, NULL);
  956. else
  957. i = get_itt(itt);
  958. if (i >= session->cmds_max)
  959. return NULL;
  960. return session->cmds[i];
  961. }
  962. EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
  963. /**
  964. * __iscsi_complete_pdu - complete pdu
  965. * @conn: iscsi conn
  966. * @hdr: iscsi header
  967. * @data: data buffer
  968. * @datalen: len of data buffer
  969. *
  970. * Completes pdu processing by freeing any resources allocated at
  971. * queuecommand or send generic. session lock must be held and verify
  972. * itt must have been called.
  973. */
  974. int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  975. char *data, int datalen)
  976. {
  977. struct iscsi_session *session = conn->session;
  978. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  979. struct iscsi_task *task;
  980. uint32_t itt;
  981. conn->last_recv = jiffies;
  982. rc = iscsi_verify_itt(conn, hdr->itt);
  983. if (rc)
  984. return rc;
  985. if (hdr->itt != RESERVED_ITT)
  986. itt = get_itt(hdr->itt);
  987. else
  988. itt = ~0U;
  989. ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
  990. opcode, conn->id, itt, datalen);
  991. if (itt == ~0U) {
  992. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  993. switch(opcode) {
  994. case ISCSI_OP_NOOP_IN:
  995. if (datalen) {
  996. rc = ISCSI_ERR_PROTO;
  997. break;
  998. }
  999. if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
  1000. break;
  1001. iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
  1002. break;
  1003. case ISCSI_OP_REJECT:
  1004. rc = iscsi_handle_reject(conn, hdr, data, datalen);
  1005. break;
  1006. case ISCSI_OP_ASYNC_EVENT:
  1007. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  1008. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  1009. rc = ISCSI_ERR_CONN_FAILED;
  1010. break;
  1011. default:
  1012. rc = ISCSI_ERR_BAD_OPCODE;
  1013. break;
  1014. }
  1015. goto out;
  1016. }
  1017. switch(opcode) {
  1018. case ISCSI_OP_SCSI_CMD_RSP:
  1019. case ISCSI_OP_SCSI_DATA_IN:
  1020. task = iscsi_itt_to_ctask(conn, hdr->itt);
  1021. if (!task)
  1022. return ISCSI_ERR_BAD_ITT;
  1023. task->last_xfer = jiffies;
  1024. break;
  1025. case ISCSI_OP_R2T:
  1026. /*
  1027. * LLD handles R2Ts if they need to.
  1028. */
  1029. return 0;
  1030. case ISCSI_OP_LOGOUT_RSP:
  1031. case ISCSI_OP_LOGIN_RSP:
  1032. case ISCSI_OP_TEXT_RSP:
  1033. case ISCSI_OP_SCSI_TMFUNC_RSP:
  1034. case ISCSI_OP_NOOP_IN:
  1035. task = iscsi_itt_to_task(conn, hdr->itt);
  1036. if (!task)
  1037. return ISCSI_ERR_BAD_ITT;
  1038. break;
  1039. default:
  1040. return ISCSI_ERR_BAD_OPCODE;
  1041. }
  1042. switch(opcode) {
  1043. case ISCSI_OP_SCSI_CMD_RSP:
  1044. iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
  1045. break;
  1046. case ISCSI_OP_SCSI_DATA_IN:
  1047. iscsi_data_in_rsp(conn, hdr, task);
  1048. break;
  1049. case ISCSI_OP_LOGOUT_RSP:
  1050. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1051. if (datalen) {
  1052. rc = ISCSI_ERR_PROTO;
  1053. break;
  1054. }
  1055. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  1056. goto recv_pdu;
  1057. case ISCSI_OP_LOGIN_RSP:
  1058. case ISCSI_OP_TEXT_RSP:
  1059. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1060. /*
  1061. * login related PDU's exp_statsn is handled in
  1062. * userspace
  1063. */
  1064. goto recv_pdu;
  1065. case ISCSI_OP_SCSI_TMFUNC_RSP:
  1066. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1067. if (datalen) {
  1068. rc = ISCSI_ERR_PROTO;
  1069. break;
  1070. }
  1071. iscsi_tmf_rsp(conn, hdr);
  1072. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  1073. break;
  1074. case ISCSI_OP_NOOP_IN:
  1075. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1076. if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
  1077. rc = ISCSI_ERR_PROTO;
  1078. break;
  1079. }
  1080. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  1081. rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
  1082. data, datalen);
  1083. break;
  1084. default:
  1085. rc = ISCSI_ERR_BAD_OPCODE;
  1086. break;
  1087. }
  1088. out:
  1089. return rc;
  1090. recv_pdu:
  1091. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  1092. rc = ISCSI_ERR_CONN_FAILED;
  1093. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  1094. return rc;
  1095. }
  1096. EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
  1097. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  1098. char *data, int datalen)
  1099. {
  1100. int rc;
  1101. spin_lock(&conn->session->lock);
  1102. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  1103. spin_unlock(&conn->session->lock);
  1104. return rc;
  1105. }
  1106. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  1107. int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
  1108. {
  1109. struct iscsi_session *session = conn->session;
  1110. int age = 0, i = 0;
  1111. if (itt == RESERVED_ITT)
  1112. return 0;
  1113. if (session->tt->parse_pdu_itt)
  1114. session->tt->parse_pdu_itt(conn, itt, &i, &age);
  1115. else {
  1116. i = get_itt(itt);
  1117. age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
  1118. }
  1119. if (age != session->age) {
  1120. iscsi_conn_printk(KERN_ERR, conn,
  1121. "received itt %x expected session age (%x)\n",
  1122. (__force u32)itt, session->age);
  1123. return ISCSI_ERR_BAD_ITT;
  1124. }
  1125. if (i >= session->cmds_max) {
  1126. iscsi_conn_printk(KERN_ERR, conn,
  1127. "received invalid itt index %u (max cmds "
  1128. "%u.\n", i, session->cmds_max);
  1129. return ISCSI_ERR_BAD_ITT;
  1130. }
  1131. return 0;
  1132. }
  1133. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  1134. /**
  1135. * iscsi_itt_to_ctask - look up ctask by itt
  1136. * @conn: iscsi connection
  1137. * @itt: itt
  1138. *
  1139. * This should be used for cmd tasks.
  1140. *
  1141. * The session lock must be held.
  1142. */
  1143. struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
  1144. {
  1145. struct iscsi_task *task;
  1146. if (iscsi_verify_itt(conn, itt))
  1147. return NULL;
  1148. task = iscsi_itt_to_task(conn, itt);
  1149. if (!task || !task->sc)
  1150. return NULL;
  1151. if (task->sc->SCp.phase != conn->session->age) {
  1152. iscsi_session_printk(KERN_ERR, conn->session,
  1153. "task's session age %d, expected %d\n",
  1154. task->sc->SCp.phase, conn->session->age);
  1155. return NULL;
  1156. }
  1157. return task;
  1158. }
  1159. EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
  1160. void iscsi_session_failure(struct iscsi_session *session,
  1161. enum iscsi_err err)
  1162. {
  1163. struct iscsi_conn *conn;
  1164. struct device *dev;
  1165. spin_lock_bh(&session->lock);
  1166. conn = session->leadconn;
  1167. if (session->state == ISCSI_STATE_TERMINATE || !conn) {
  1168. spin_unlock_bh(&session->lock);
  1169. return;
  1170. }
  1171. dev = get_device(&conn->cls_conn->dev);
  1172. spin_unlock_bh(&session->lock);
  1173. if (!dev)
  1174. return;
  1175. /*
  1176. * if the host is being removed bypass the connection
  1177. * recovery initialization because we are going to kill
  1178. * the session.
  1179. */
  1180. if (err == ISCSI_ERR_INVALID_HOST)
  1181. iscsi_conn_error_event(conn->cls_conn, err);
  1182. else
  1183. iscsi_conn_failure(conn, err);
  1184. put_device(dev);
  1185. }
  1186. EXPORT_SYMBOL_GPL(iscsi_session_failure);
  1187. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  1188. {
  1189. struct iscsi_session *session = conn->session;
  1190. spin_lock_bh(&session->lock);
  1191. if (session->state == ISCSI_STATE_FAILED) {
  1192. spin_unlock_bh(&session->lock);
  1193. return;
  1194. }
  1195. if (conn->stop_stage == 0)
  1196. session->state = ISCSI_STATE_FAILED;
  1197. spin_unlock_bh(&session->lock);
  1198. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1199. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1200. iscsi_conn_error_event(conn->cls_conn, err);
  1201. }
  1202. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  1203. static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
  1204. {
  1205. struct iscsi_session *session = conn->session;
  1206. /*
  1207. * Check for iSCSI window and take care of CmdSN wrap-around
  1208. */
  1209. if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
  1210. ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
  1211. "%u MaxCmdSN %u CmdSN %u/%u\n",
  1212. session->exp_cmdsn, session->max_cmdsn,
  1213. session->cmdsn, session->queued_cmdsn);
  1214. return -ENOSPC;
  1215. }
  1216. return 0;
  1217. }
  1218. static int iscsi_xmit_task(struct iscsi_conn *conn)
  1219. {
  1220. struct iscsi_task *task = conn->task;
  1221. int rc;
  1222. if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
  1223. return -ENODATA;
  1224. __iscsi_get_task(task);
  1225. spin_unlock_bh(&conn->session->lock);
  1226. rc = conn->session->tt->xmit_task(task);
  1227. spin_lock_bh(&conn->session->lock);
  1228. if (!rc) {
  1229. /* done with this task */
  1230. task->last_xfer = jiffies;
  1231. conn->task = NULL;
  1232. }
  1233. __iscsi_put_task(task);
  1234. return rc;
  1235. }
  1236. /**
  1237. * iscsi_requeue_task - requeue task to run from session workqueue
  1238. * @task: task to requeue
  1239. *
  1240. * LLDs that need to run a task from the session workqueue should call
  1241. * this. The session lock must be held. This should only be called
  1242. * by software drivers.
  1243. */
  1244. void iscsi_requeue_task(struct iscsi_task *task)
  1245. {
  1246. struct iscsi_conn *conn = task->conn;
  1247. /*
  1248. * this may be on the requeue list already if the xmit_task callout
  1249. * is handling the r2ts while we are adding new ones
  1250. */
  1251. if (list_empty(&task->running))
  1252. list_add_tail(&task->running, &conn->requeue);
  1253. iscsi_conn_queue_work(conn);
  1254. }
  1255. EXPORT_SYMBOL_GPL(iscsi_requeue_task);
  1256. /**
  1257. * iscsi_data_xmit - xmit any command into the scheduled connection
  1258. * @conn: iscsi connection
  1259. *
  1260. * Notes:
  1261. * The function can return -EAGAIN in which case the caller must
  1262. * re-schedule it again later or recover. '0' return code means
  1263. * successful xmit.
  1264. **/
  1265. static int iscsi_data_xmit(struct iscsi_conn *conn)
  1266. {
  1267. struct iscsi_task *task;
  1268. int rc = 0;
  1269. spin_lock_bh(&conn->session->lock);
  1270. if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
  1271. ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
  1272. spin_unlock_bh(&conn->session->lock);
  1273. return -ENODATA;
  1274. }
  1275. if (conn->task) {
  1276. rc = iscsi_xmit_task(conn);
  1277. if (rc)
  1278. goto done;
  1279. }
  1280. /*
  1281. * process mgmt pdus like nops before commands since we should
  1282. * only have one nop-out as a ping from us and targets should not
  1283. * overflow us with nop-ins
  1284. */
  1285. check_mgmt:
  1286. while (!list_empty(&conn->mgmtqueue)) {
  1287. conn->task = list_entry(conn->mgmtqueue.next,
  1288. struct iscsi_task, running);
  1289. list_del_init(&conn->task->running);
  1290. if (iscsi_prep_mgmt_task(conn, conn->task)) {
  1291. __iscsi_put_task(conn->task);
  1292. conn->task = NULL;
  1293. continue;
  1294. }
  1295. rc = iscsi_xmit_task(conn);
  1296. if (rc)
  1297. goto done;
  1298. }
  1299. /* process pending command queue */
  1300. while (!list_empty(&conn->cmdqueue)) {
  1301. conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task,
  1302. running);
  1303. list_del_init(&conn->task->running);
  1304. if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
  1305. fail_scsi_task(conn->task, DID_IMM_RETRY);
  1306. continue;
  1307. }
  1308. rc = iscsi_prep_scsi_cmd_pdu(conn->task);
  1309. if (rc) {
  1310. if (rc == -ENOMEM || rc == -EACCES) {
  1311. list_add_tail(&conn->task->running,
  1312. &conn->cmdqueue);
  1313. conn->task = NULL;
  1314. goto done;
  1315. } else
  1316. fail_scsi_task(conn->task, DID_ABORT);
  1317. continue;
  1318. }
  1319. rc = iscsi_xmit_task(conn);
  1320. if (rc)
  1321. goto done;
  1322. /*
  1323. * we could continuously get new task requests so
  1324. * we need to check the mgmt queue for nops that need to
  1325. * be sent to aviod starvation
  1326. */
  1327. if (!list_empty(&conn->mgmtqueue))
  1328. goto check_mgmt;
  1329. }
  1330. while (!list_empty(&conn->requeue)) {
  1331. /*
  1332. * we always do fastlogout - conn stop code will clean up.
  1333. */
  1334. if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
  1335. break;
  1336. task = list_entry(conn->requeue.next, struct iscsi_task,
  1337. running);
  1338. if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
  1339. break;
  1340. conn->task = task;
  1341. list_del_init(&conn->task->running);
  1342. conn->task->state = ISCSI_TASK_RUNNING;
  1343. rc = iscsi_xmit_task(conn);
  1344. if (rc)
  1345. goto done;
  1346. if (!list_empty(&conn->mgmtqueue))
  1347. goto check_mgmt;
  1348. }
  1349. spin_unlock_bh(&conn->session->lock);
  1350. return -ENODATA;
  1351. done:
  1352. spin_unlock_bh(&conn->session->lock);
  1353. return rc;
  1354. }
  1355. static void iscsi_xmitworker(struct work_struct *work)
  1356. {
  1357. struct iscsi_conn *conn =
  1358. container_of(work, struct iscsi_conn, xmitwork);
  1359. int rc;
  1360. /*
  1361. * serialize Xmit worker on a per-connection basis.
  1362. */
  1363. do {
  1364. rc = iscsi_data_xmit(conn);
  1365. } while (rc >= 0 || rc == -EAGAIN);
  1366. }
  1367. static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
  1368. struct scsi_cmnd *sc)
  1369. {
  1370. struct iscsi_task *task;
  1371. if (!kfifo_out(&conn->session->cmdpool.queue,
  1372. (void *) &task, sizeof(void *)))
  1373. return NULL;
  1374. sc->SCp.phase = conn->session->age;
  1375. sc->SCp.ptr = (char *) task;
  1376. atomic_set(&task->refcount, 1);
  1377. task->state = ISCSI_TASK_PENDING;
  1378. task->conn = conn;
  1379. task->sc = sc;
  1380. task->have_checked_conn = false;
  1381. task->last_timeout = jiffies;
  1382. task->last_xfer = jiffies;
  1383. INIT_LIST_HEAD(&task->running);
  1384. return task;
  1385. }
  1386. enum {
  1387. FAILURE_BAD_HOST = 1,
  1388. FAILURE_SESSION_FAILED,
  1389. FAILURE_SESSION_FREED,
  1390. FAILURE_WINDOW_CLOSED,
  1391. FAILURE_OOM,
  1392. FAILURE_SESSION_TERMINATE,
  1393. FAILURE_SESSION_IN_RECOVERY,
  1394. FAILURE_SESSION_RECOVERY_TIMEOUT,
  1395. FAILURE_SESSION_LOGGING_OUT,
  1396. FAILURE_SESSION_NOT_READY,
  1397. };
  1398. int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc)
  1399. {
  1400. struct iscsi_cls_session *cls_session;
  1401. struct iscsi_host *ihost;
  1402. int reason = 0;
  1403. struct iscsi_session *session;
  1404. struct iscsi_conn *conn;
  1405. struct iscsi_task *task = NULL;
  1406. sc->result = 0;
  1407. sc->SCp.ptr = NULL;
  1408. ihost = shost_priv(host);
  1409. cls_session = starget_to_session(scsi_target(sc->device));
  1410. session = cls_session->dd_data;
  1411. spin_lock_bh(&session->lock);
  1412. reason = iscsi_session_chkready(cls_session);
  1413. if (reason) {
  1414. sc->result = reason;
  1415. goto fault;
  1416. }
  1417. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1418. /*
  1419. * to handle the race between when we set the recovery state
  1420. * and block the session we requeue here (commands could
  1421. * be entering our queuecommand while a block is starting
  1422. * up because the block code is not locked)
  1423. */
  1424. switch (session->state) {
  1425. case ISCSI_STATE_FAILED:
  1426. case ISCSI_STATE_IN_RECOVERY:
  1427. reason = FAILURE_SESSION_IN_RECOVERY;
  1428. sc->result = DID_IMM_RETRY << 16;
  1429. break;
  1430. case ISCSI_STATE_LOGGING_OUT:
  1431. reason = FAILURE_SESSION_LOGGING_OUT;
  1432. sc->result = DID_IMM_RETRY << 16;
  1433. break;
  1434. case ISCSI_STATE_RECOVERY_FAILED:
  1435. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  1436. sc->result = DID_TRANSPORT_FAILFAST << 16;
  1437. break;
  1438. case ISCSI_STATE_TERMINATE:
  1439. reason = FAILURE_SESSION_TERMINATE;
  1440. sc->result = DID_NO_CONNECT << 16;
  1441. break;
  1442. default:
  1443. reason = FAILURE_SESSION_FREED;
  1444. sc->result = DID_NO_CONNECT << 16;
  1445. }
  1446. goto fault;
  1447. }
  1448. conn = session->leadconn;
  1449. if (!conn) {
  1450. reason = FAILURE_SESSION_FREED;
  1451. sc->result = DID_NO_CONNECT << 16;
  1452. goto fault;
  1453. }
  1454. if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
  1455. reason = FAILURE_SESSION_IN_RECOVERY;
  1456. sc->result = DID_REQUEUE;
  1457. goto fault;
  1458. }
  1459. if (iscsi_check_cmdsn_window_closed(conn)) {
  1460. reason = FAILURE_WINDOW_CLOSED;
  1461. goto reject;
  1462. }
  1463. task = iscsi_alloc_task(conn, sc);
  1464. if (!task) {
  1465. reason = FAILURE_OOM;
  1466. goto reject;
  1467. }
  1468. if (!ihost->workq) {
  1469. reason = iscsi_prep_scsi_cmd_pdu(task);
  1470. if (reason) {
  1471. if (reason == -ENOMEM || reason == -EACCES) {
  1472. reason = FAILURE_OOM;
  1473. goto prepd_reject;
  1474. } else {
  1475. sc->result = DID_ABORT << 16;
  1476. goto prepd_fault;
  1477. }
  1478. }
  1479. if (session->tt->xmit_task(task)) {
  1480. session->cmdsn--;
  1481. reason = FAILURE_SESSION_NOT_READY;
  1482. goto prepd_reject;
  1483. }
  1484. } else {
  1485. list_add_tail(&task->running, &conn->cmdqueue);
  1486. iscsi_conn_queue_work(conn);
  1487. }
  1488. session->queued_cmdsn++;
  1489. spin_unlock_bh(&session->lock);
  1490. return 0;
  1491. prepd_reject:
  1492. iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
  1493. reject:
  1494. spin_unlock_bh(&session->lock);
  1495. ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
  1496. sc->cmnd[0], reason);
  1497. return SCSI_MLQUEUE_TARGET_BUSY;
  1498. prepd_fault:
  1499. iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
  1500. fault:
  1501. spin_unlock_bh(&session->lock);
  1502. ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
  1503. sc->cmnd[0], reason);
  1504. if (!scsi_bidi_cmnd(sc))
  1505. scsi_set_resid(sc, scsi_bufflen(sc));
  1506. else {
  1507. scsi_out(sc)->resid = scsi_out(sc)->length;
  1508. scsi_in(sc)->resid = scsi_in(sc)->length;
  1509. }
  1510. sc->scsi_done(sc);
  1511. return 0;
  1512. }
  1513. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  1514. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
  1515. {
  1516. switch (reason) {
  1517. case SCSI_QDEPTH_DEFAULT:
  1518. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  1519. break;
  1520. case SCSI_QDEPTH_QFULL:
  1521. scsi_track_queue_full(sdev, depth);
  1522. break;
  1523. case SCSI_QDEPTH_RAMP_UP:
  1524. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  1525. break;
  1526. default:
  1527. return -EOPNOTSUPP;
  1528. }
  1529. return sdev->queue_depth;
  1530. }
  1531. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  1532. int iscsi_target_alloc(struct scsi_target *starget)
  1533. {
  1534. struct iscsi_cls_session *cls_session = starget_to_session(starget);
  1535. struct iscsi_session *session = cls_session->dd_data;
  1536. starget->can_queue = session->scsi_cmds_max;
  1537. return 0;
  1538. }
  1539. EXPORT_SYMBOL_GPL(iscsi_target_alloc);
  1540. static void iscsi_tmf_timedout(unsigned long data)
  1541. {
  1542. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1543. struct iscsi_session *session = conn->session;
  1544. spin_lock(&session->lock);
  1545. if (conn->tmf_state == TMF_QUEUED) {
  1546. conn->tmf_state = TMF_TIMEDOUT;
  1547. ISCSI_DBG_EH(session, "tmf timedout\n");
  1548. /* unblock eh_abort() */
  1549. wake_up(&conn->ehwait);
  1550. }
  1551. spin_unlock(&session->lock);
  1552. }
  1553. static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
  1554. struct iscsi_tm *hdr, int age,
  1555. int timeout)
  1556. {
  1557. struct iscsi_session *session = conn->session;
  1558. struct iscsi_task *task;
  1559. task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
  1560. NULL, 0);
  1561. if (!task) {
  1562. spin_unlock_bh(&session->lock);
  1563. iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
  1564. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1565. spin_lock_bh(&session->lock);
  1566. return -EPERM;
  1567. }
  1568. conn->tmfcmd_pdus_cnt++;
  1569. conn->tmf_timer.expires = timeout * HZ + jiffies;
  1570. conn->tmf_timer.function = iscsi_tmf_timedout;
  1571. conn->tmf_timer.data = (unsigned long)conn;
  1572. add_timer(&conn->tmf_timer);
  1573. ISCSI_DBG_EH(session, "tmf set timeout\n");
  1574. spin_unlock_bh(&session->lock);
  1575. mutex_unlock(&session->eh_mutex);
  1576. /*
  1577. * block eh thread until:
  1578. *
  1579. * 1) tmf response
  1580. * 2) tmf timeout
  1581. * 3) session is terminated or restarted or userspace has
  1582. * given up on recovery
  1583. */
  1584. wait_event_interruptible(conn->ehwait, age != session->age ||
  1585. session->state != ISCSI_STATE_LOGGED_IN ||
  1586. conn->tmf_state != TMF_QUEUED);
  1587. if (signal_pending(current))
  1588. flush_signals(current);
  1589. del_timer_sync(&conn->tmf_timer);
  1590. mutex_lock(&session->eh_mutex);
  1591. spin_lock_bh(&session->lock);
  1592. /* if the session drops it will clean up the task */
  1593. if (age != session->age ||
  1594. session->state != ISCSI_STATE_LOGGED_IN)
  1595. return -ENOTCONN;
  1596. return 0;
  1597. }
  1598. /*
  1599. * Fail commands. session lock held and recv side suspended and xmit
  1600. * thread flushed
  1601. */
  1602. static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
  1603. int error)
  1604. {
  1605. struct iscsi_task *task;
  1606. int i;
  1607. for (i = 0; i < conn->session->cmds_max; i++) {
  1608. task = conn->session->cmds[i];
  1609. if (!task->sc || task->state == ISCSI_TASK_FREE)
  1610. continue;
  1611. if (lun != -1 && lun != task->sc->device->lun)
  1612. continue;
  1613. ISCSI_DBG_SESSION(conn->session,
  1614. "failing sc %p itt 0x%x state %d\n",
  1615. task->sc, task->itt, task->state);
  1616. fail_scsi_task(task, error);
  1617. }
  1618. }
  1619. /**
  1620. * iscsi_suspend_queue - suspend iscsi_queuecommand
  1621. * @conn: iscsi conn to stop queueing IO on
  1622. *
  1623. * This grabs the session lock to make sure no one is in
  1624. * xmit_task/queuecommand, and then sets suspend to prevent
  1625. * new commands from being queued. This only needs to be called
  1626. * by offload drivers that need to sync a path like ep disconnect
  1627. * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
  1628. * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
  1629. */
  1630. void iscsi_suspend_queue(struct iscsi_conn *conn)
  1631. {
  1632. spin_lock_bh(&conn->session->lock);
  1633. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1634. spin_unlock_bh(&conn->session->lock);
  1635. }
  1636. EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
  1637. /**
  1638. * iscsi_suspend_tx - suspend iscsi_data_xmit
  1639. * @conn: iscsi conn tp stop processing IO on.
  1640. *
  1641. * This function sets the suspend bit to prevent iscsi_data_xmit
  1642. * from sending new IO, and if work is queued on the xmit thread
  1643. * it will wait for it to be completed.
  1644. */
  1645. void iscsi_suspend_tx(struct iscsi_conn *conn)
  1646. {
  1647. struct Scsi_Host *shost = conn->session->host;
  1648. struct iscsi_host *ihost = shost_priv(shost);
  1649. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1650. if (ihost->workq)
  1651. flush_workqueue(ihost->workq);
  1652. }
  1653. EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
  1654. static void iscsi_start_tx(struct iscsi_conn *conn)
  1655. {
  1656. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1657. iscsi_conn_queue_work(conn);
  1658. }
  1659. /*
  1660. * We want to make sure a ping is in flight. It has timed out.
  1661. * And we are not busy processing a pdu that is making
  1662. * progress but got started before the ping and is taking a while
  1663. * to complete so the ping is just stuck behind it in a queue.
  1664. */
  1665. static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
  1666. {
  1667. if (conn->ping_task &&
  1668. time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
  1669. (conn->ping_timeout * HZ), jiffies))
  1670. return 1;
  1671. else
  1672. return 0;
  1673. }
  1674. static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
  1675. {
  1676. enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
  1677. struct iscsi_task *task = NULL, *running_task;
  1678. struct iscsi_cls_session *cls_session;
  1679. struct iscsi_session *session;
  1680. struct iscsi_conn *conn;
  1681. int i;
  1682. cls_session = starget_to_session(scsi_target(sc->device));
  1683. session = cls_session->dd_data;
  1684. ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
  1685. spin_lock(&session->lock);
  1686. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1687. /*
  1688. * We are probably in the middle of iscsi recovery so let
  1689. * that complete and handle the error.
  1690. */
  1691. rc = BLK_EH_RESET_TIMER;
  1692. goto done;
  1693. }
  1694. conn = session->leadconn;
  1695. if (!conn) {
  1696. /* In the middle of shuting down */
  1697. rc = BLK_EH_RESET_TIMER;
  1698. goto done;
  1699. }
  1700. task = (struct iscsi_task *)sc->SCp.ptr;
  1701. if (!task) {
  1702. /*
  1703. * Raced with completion. Just reset timer, and let it
  1704. * complete normally
  1705. */
  1706. rc = BLK_EH_RESET_TIMER;
  1707. goto done;
  1708. }
  1709. /*
  1710. * If we have sent (at least queued to the network layer) a pdu or
  1711. * recvd one for the task since the last timeout ask for
  1712. * more time. If on the next timeout we have not made progress
  1713. * we can check if it is the task or connection when we send the
  1714. * nop as a ping.
  1715. */
  1716. if (time_after(task->last_xfer, task->last_timeout)) {
  1717. ISCSI_DBG_EH(session, "Command making progress. Asking "
  1718. "scsi-ml for more time to complete. "
  1719. "Last data xfer at %lu. Last timeout was at "
  1720. "%lu\n.", task->last_xfer, task->last_timeout);
  1721. task->have_checked_conn = false;
  1722. rc = BLK_EH_RESET_TIMER;
  1723. goto done;
  1724. }
  1725. if (!conn->recv_timeout && !conn->ping_timeout)
  1726. goto done;
  1727. /*
  1728. * if the ping timedout then we are in the middle of cleaning up
  1729. * and can let the iscsi eh handle it
  1730. */
  1731. if (iscsi_has_ping_timed_out(conn)) {
  1732. rc = BLK_EH_RESET_TIMER;
  1733. goto done;
  1734. }
  1735. for (i = 0; i < conn->session->cmds_max; i++) {
  1736. running_task = conn->session->cmds[i];
  1737. if (!running_task->sc || running_task == task ||
  1738. running_task->state != ISCSI_TASK_RUNNING)
  1739. continue;
  1740. /*
  1741. * Only check if cmds started before this one have made
  1742. * progress, or this could never fail
  1743. */
  1744. if (time_after(running_task->sc->jiffies_at_alloc,
  1745. task->sc->jiffies_at_alloc))
  1746. continue;
  1747. if (time_after(running_task->last_xfer, task->last_timeout)) {
  1748. /*
  1749. * This task has not made progress, but a task
  1750. * started before us has transferred data since
  1751. * we started/last-checked. We could be queueing
  1752. * too many tasks or the LU is bad.
  1753. *
  1754. * If the device is bad the cmds ahead of us on
  1755. * other devs will complete, and this loop will
  1756. * eventually fail starting the scsi eh.
  1757. */
  1758. ISCSI_DBG_EH(session, "Command has not made progress "
  1759. "but commands ahead of it have. "
  1760. "Asking scsi-ml for more time to "
  1761. "complete. Our last xfer vs running task "
  1762. "last xfer %lu/%lu. Last check %lu.\n",
  1763. task->last_xfer, running_task->last_xfer,
  1764. task->last_timeout);
  1765. rc = BLK_EH_RESET_TIMER;
  1766. goto done;
  1767. }
  1768. }
  1769. /* Assumes nop timeout is shorter than scsi cmd timeout */
  1770. if (task->have_checked_conn)
  1771. goto done;
  1772. /*
  1773. * Checking the transport already or nop from a cmd timeout still
  1774. * running
  1775. */
  1776. if (conn->ping_task) {
  1777. task->have_checked_conn = true;
  1778. rc = BLK_EH_RESET_TIMER;
  1779. goto done;
  1780. }
  1781. /* Make sure there is a transport check done */
  1782. iscsi_send_nopout(conn, NULL);
  1783. task->have_checked_conn = true;
  1784. rc = BLK_EH_RESET_TIMER;
  1785. done:
  1786. if (task)
  1787. task->last_timeout = jiffies;
  1788. spin_unlock(&session->lock);
  1789. ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
  1790. "timer reset" : "nh");
  1791. return rc;
  1792. }
  1793. static void iscsi_check_transport_timeouts(unsigned long data)
  1794. {
  1795. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1796. struct iscsi_session *session = conn->session;
  1797. unsigned long recv_timeout, next_timeout = 0, last_recv;
  1798. spin_lock(&session->lock);
  1799. if (session->state != ISCSI_STATE_LOGGED_IN)
  1800. goto done;
  1801. recv_timeout = conn->recv_timeout;
  1802. if (!recv_timeout)
  1803. goto done;
  1804. recv_timeout *= HZ;
  1805. last_recv = conn->last_recv;
  1806. if (iscsi_has_ping_timed_out(conn)) {
  1807. iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
  1808. "expired, recv timeout %d, last rx %lu, "
  1809. "last ping %lu, now %lu\n",
  1810. conn->ping_timeout, conn->recv_timeout,
  1811. last_recv, conn->last_ping, jiffies);
  1812. spin_unlock(&session->lock);
  1813. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1814. return;
  1815. }
  1816. if (time_before_eq(last_recv + recv_timeout, jiffies)) {
  1817. /* send a ping to try to provoke some traffic */
  1818. ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
  1819. iscsi_send_nopout(conn, NULL);
  1820. next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
  1821. } else
  1822. next_timeout = last_recv + recv_timeout;
  1823. ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
  1824. mod_timer(&conn->transport_timer, next_timeout);
  1825. done:
  1826. spin_unlock(&session->lock);
  1827. }
  1828. static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
  1829. struct iscsi_tm *hdr)
  1830. {
  1831. memset(hdr, 0, sizeof(*hdr));
  1832. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1833. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
  1834. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1835. hdr->lun = task->lun;
  1836. hdr->rtt = task->hdr_itt;
  1837. hdr->refcmdsn = task->cmdsn;
  1838. }
  1839. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1840. {
  1841. struct iscsi_cls_session *cls_session;
  1842. struct iscsi_session *session;
  1843. struct iscsi_conn *conn;
  1844. struct iscsi_task *task;
  1845. struct iscsi_tm *hdr;
  1846. int rc, age;
  1847. cls_session = starget_to_session(scsi_target(sc->device));
  1848. session = cls_session->dd_data;
  1849. ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
  1850. mutex_lock(&session->eh_mutex);
  1851. spin_lock_bh(&session->lock);
  1852. /*
  1853. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1854. * got the command.
  1855. */
  1856. if (!sc->SCp.ptr) {
  1857. ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
  1858. "it completed.\n");
  1859. spin_unlock_bh(&session->lock);
  1860. mutex_unlock(&session->eh_mutex);
  1861. return SUCCESS;
  1862. }
  1863. /*
  1864. * If we are not logged in or we have started a new session
  1865. * then let the host reset code handle this
  1866. */
  1867. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
  1868. sc->SCp.phase != session->age) {
  1869. spin_unlock_bh(&session->lock);
  1870. mutex_unlock(&session->eh_mutex);
  1871. ISCSI_DBG_EH(session, "failing abort due to dropped "
  1872. "session.\n");
  1873. return FAILED;
  1874. }
  1875. conn = session->leadconn;
  1876. conn->eh_abort_cnt++;
  1877. age = session->age;
  1878. task = (struct iscsi_task *)sc->SCp.ptr;
  1879. ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
  1880. sc, task->itt);
  1881. /* task completed before time out */
  1882. if (!task->sc) {
  1883. ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
  1884. goto success;
  1885. }
  1886. if (task->state == ISCSI_TASK_PENDING) {
  1887. fail_scsi_task(task, DID_ABORT);
  1888. goto success;
  1889. }
  1890. /* only have one tmf outstanding at a time */
  1891. if (conn->tmf_state != TMF_INITIAL)
  1892. goto failed;
  1893. conn->tmf_state = TMF_QUEUED;
  1894. hdr = &conn->tmhdr;
  1895. iscsi_prep_abort_task_pdu(task, hdr);
  1896. if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
  1897. rc = FAILED;
  1898. goto failed;
  1899. }
  1900. switch (conn->tmf_state) {
  1901. case TMF_SUCCESS:
  1902. spin_unlock_bh(&session->lock);
  1903. /*
  1904. * stop tx side incase the target had sent a abort rsp but
  1905. * the initiator was still writing out data.
  1906. */
  1907. iscsi_suspend_tx(conn);
  1908. /*
  1909. * we do not stop the recv side because targets have been
  1910. * good and have never sent us a successful tmf response
  1911. * then sent more data for the cmd.
  1912. */
  1913. spin_lock_bh(&session->lock);
  1914. fail_scsi_task(task, DID_ABORT);
  1915. conn->tmf_state = TMF_INITIAL;
  1916. memset(hdr, 0, sizeof(*hdr));
  1917. spin_unlock_bh(&session->lock);
  1918. iscsi_start_tx(conn);
  1919. goto success_unlocked;
  1920. case TMF_TIMEDOUT:
  1921. spin_unlock_bh(&session->lock);
  1922. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  1923. goto failed_unlocked;
  1924. case TMF_NOT_FOUND:
  1925. if (!sc->SCp.ptr) {
  1926. conn->tmf_state = TMF_INITIAL;
  1927. memset(hdr, 0, sizeof(*hdr));
  1928. /* task completed before tmf abort response */
  1929. ISCSI_DBG_EH(session, "sc completed while abort in "
  1930. "progress\n");
  1931. goto success;
  1932. }
  1933. /* fall through */
  1934. default:
  1935. conn->tmf_state = TMF_INITIAL;
  1936. goto failed;
  1937. }
  1938. success:
  1939. spin_unlock_bh(&session->lock);
  1940. success_unlocked:
  1941. ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
  1942. sc, task->itt);
  1943. mutex_unlock(&session->eh_mutex);
  1944. return SUCCESS;
  1945. failed:
  1946. spin_unlock_bh(&session->lock);
  1947. failed_unlocked:
  1948. ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
  1949. task ? task->itt : 0);
  1950. mutex_unlock(&session->eh_mutex);
  1951. return FAILED;
  1952. }
  1953. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1954. static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  1955. {
  1956. memset(hdr, 0, sizeof(*hdr));
  1957. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1958. hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  1959. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1960. int_to_scsilun(sc->device->lun, &hdr->lun);
  1961. hdr->rtt = RESERVED_ITT;
  1962. }
  1963. int iscsi_eh_device_reset(struct scsi_cmnd *sc)
  1964. {
  1965. struct iscsi_cls_session *cls_session;
  1966. struct iscsi_session *session;
  1967. struct iscsi_conn *conn;
  1968. struct iscsi_tm *hdr;
  1969. int rc = FAILED;
  1970. cls_session = starget_to_session(scsi_target(sc->device));
  1971. session = cls_session->dd_data;
  1972. ISCSI_DBG_EH(session, "LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
  1973. mutex_lock(&session->eh_mutex);
  1974. spin_lock_bh(&session->lock);
  1975. /*
  1976. * Just check if we are not logged in. We cannot check for
  1977. * the phase because the reset could come from a ioctl.
  1978. */
  1979. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  1980. goto unlock;
  1981. conn = session->leadconn;
  1982. /* only have one tmf outstanding at a time */
  1983. if (conn->tmf_state != TMF_INITIAL)
  1984. goto unlock;
  1985. conn->tmf_state = TMF_QUEUED;
  1986. hdr = &conn->tmhdr;
  1987. iscsi_prep_lun_reset_pdu(sc, hdr);
  1988. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
  1989. session->lu_reset_timeout)) {
  1990. rc = FAILED;
  1991. goto unlock;
  1992. }
  1993. switch (conn->tmf_state) {
  1994. case TMF_SUCCESS:
  1995. break;
  1996. case TMF_TIMEDOUT:
  1997. spin_unlock_bh(&session->lock);
  1998. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  1999. goto done;
  2000. default:
  2001. conn->tmf_state = TMF_INITIAL;
  2002. goto unlock;
  2003. }
  2004. rc = SUCCESS;
  2005. spin_unlock_bh(&session->lock);
  2006. iscsi_suspend_tx(conn);
  2007. spin_lock_bh(&session->lock);
  2008. memset(hdr, 0, sizeof(*hdr));
  2009. fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
  2010. conn->tmf_state = TMF_INITIAL;
  2011. spin_unlock_bh(&session->lock);
  2012. iscsi_start_tx(conn);
  2013. goto done;
  2014. unlock:
  2015. spin_unlock_bh(&session->lock);
  2016. done:
  2017. ISCSI_DBG_EH(session, "dev reset result = %s\n",
  2018. rc == SUCCESS ? "SUCCESS" : "FAILED");
  2019. mutex_unlock(&session->eh_mutex);
  2020. return rc;
  2021. }
  2022. EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
  2023. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  2024. {
  2025. struct iscsi_session *session = cls_session->dd_data;
  2026. spin_lock_bh(&session->lock);
  2027. if (session->state != ISCSI_STATE_LOGGED_IN) {
  2028. session->state = ISCSI_STATE_RECOVERY_FAILED;
  2029. if (session->leadconn)
  2030. wake_up(&session->leadconn->ehwait);
  2031. }
  2032. spin_unlock_bh(&session->lock);
  2033. }
  2034. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  2035. /**
  2036. * iscsi_eh_session_reset - drop session and attempt relogin
  2037. * @sc: scsi command
  2038. *
  2039. * This function will wait for a relogin, session termination from
  2040. * userspace, or a recovery/replacement timeout.
  2041. */
  2042. int iscsi_eh_session_reset(struct scsi_cmnd *sc)
  2043. {
  2044. struct iscsi_cls_session *cls_session;
  2045. struct iscsi_session *session;
  2046. struct iscsi_conn *conn;
  2047. cls_session = starget_to_session(scsi_target(sc->device));
  2048. session = cls_session->dd_data;
  2049. conn = session->leadconn;
  2050. mutex_lock(&session->eh_mutex);
  2051. spin_lock_bh(&session->lock);
  2052. if (session->state == ISCSI_STATE_TERMINATE) {
  2053. failed:
  2054. ISCSI_DBG_EH(session,
  2055. "failing session reset: Could not log back into "
  2056. "%s, %s [age %d]\n", session->targetname,
  2057. conn->persistent_address, session->age);
  2058. spin_unlock_bh(&session->lock);
  2059. mutex_unlock(&session->eh_mutex);
  2060. return FAILED;
  2061. }
  2062. spin_unlock_bh(&session->lock);
  2063. mutex_unlock(&session->eh_mutex);
  2064. /*
  2065. * we drop the lock here but the leadconn cannot be destoyed while
  2066. * we are in the scsi eh
  2067. */
  2068. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  2069. ISCSI_DBG_EH(session, "wait for relogin\n");
  2070. wait_event_interruptible(conn->ehwait,
  2071. session->state == ISCSI_STATE_TERMINATE ||
  2072. session->state == ISCSI_STATE_LOGGED_IN ||
  2073. session->state == ISCSI_STATE_RECOVERY_FAILED);
  2074. if (signal_pending(current))
  2075. flush_signals(current);
  2076. mutex_lock(&session->eh_mutex);
  2077. spin_lock_bh(&session->lock);
  2078. if (session->state == ISCSI_STATE_LOGGED_IN) {
  2079. ISCSI_DBG_EH(session,
  2080. "session reset succeeded for %s,%s\n",
  2081. session->targetname, conn->persistent_address);
  2082. } else
  2083. goto failed;
  2084. spin_unlock_bh(&session->lock);
  2085. mutex_unlock(&session->eh_mutex);
  2086. return SUCCESS;
  2087. }
  2088. EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
  2089. static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  2090. {
  2091. memset(hdr, 0, sizeof(*hdr));
  2092. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  2093. hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  2094. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  2095. hdr->rtt = RESERVED_ITT;
  2096. }
  2097. /**
  2098. * iscsi_eh_target_reset - reset target
  2099. * @sc: scsi command
  2100. *
  2101. * This will attempt to send a warm target reset.
  2102. */
  2103. int iscsi_eh_target_reset(struct scsi_cmnd *sc)
  2104. {
  2105. struct iscsi_cls_session *cls_session;
  2106. struct iscsi_session *session;
  2107. struct iscsi_conn *conn;
  2108. struct iscsi_tm *hdr;
  2109. int rc = FAILED;
  2110. cls_session = starget_to_session(scsi_target(sc->device));
  2111. session = cls_session->dd_data;
  2112. ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
  2113. session->targetname);
  2114. mutex_lock(&session->eh_mutex);
  2115. spin_lock_bh(&session->lock);
  2116. /*
  2117. * Just check if we are not logged in. We cannot check for
  2118. * the phase because the reset could come from a ioctl.
  2119. */
  2120. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  2121. goto unlock;
  2122. conn = session->leadconn;
  2123. /* only have one tmf outstanding at a time */
  2124. if (conn->tmf_state != TMF_INITIAL)
  2125. goto unlock;
  2126. conn->tmf_state = TMF_QUEUED;
  2127. hdr = &conn->tmhdr;
  2128. iscsi_prep_tgt_reset_pdu(sc, hdr);
  2129. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
  2130. session->tgt_reset_timeout)) {
  2131. rc = FAILED;
  2132. goto unlock;
  2133. }
  2134. switch (conn->tmf_state) {
  2135. case TMF_SUCCESS:
  2136. break;
  2137. case TMF_TIMEDOUT:
  2138. spin_unlock_bh(&session->lock);
  2139. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  2140. goto done;
  2141. default:
  2142. conn->tmf_state = TMF_INITIAL;
  2143. goto unlock;
  2144. }
  2145. rc = SUCCESS;
  2146. spin_unlock_bh(&session->lock);
  2147. iscsi_suspend_tx(conn);
  2148. spin_lock_bh(&session->lock);
  2149. memset(hdr, 0, sizeof(*hdr));
  2150. fail_scsi_tasks(conn, -1, DID_ERROR);
  2151. conn->tmf_state = TMF_INITIAL;
  2152. spin_unlock_bh(&session->lock);
  2153. iscsi_start_tx(conn);
  2154. goto done;
  2155. unlock:
  2156. spin_unlock_bh(&session->lock);
  2157. done:
  2158. ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
  2159. rc == SUCCESS ? "SUCCESS" : "FAILED");
  2160. mutex_unlock(&session->eh_mutex);
  2161. return rc;
  2162. }
  2163. EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
  2164. /**
  2165. * iscsi_eh_recover_target - reset target and possibly the session
  2166. * @sc: scsi command
  2167. *
  2168. * This will attempt to send a warm target reset. If that fails,
  2169. * we will escalate to ERL0 session recovery.
  2170. */
  2171. int iscsi_eh_recover_target(struct scsi_cmnd *sc)
  2172. {
  2173. int rc;
  2174. rc = iscsi_eh_target_reset(sc);
  2175. if (rc == FAILED)
  2176. rc = iscsi_eh_session_reset(sc);
  2177. return rc;
  2178. }
  2179. EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
  2180. /*
  2181. * Pre-allocate a pool of @max items of @item_size. By default, the pool
  2182. * should be accessed via kfifo_{get,put} on q->queue.
  2183. * Optionally, the caller can obtain the array of object pointers
  2184. * by passing in a non-NULL @items pointer
  2185. */
  2186. int
  2187. iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
  2188. {
  2189. int i, num_arrays = 1;
  2190. memset(q, 0, sizeof(*q));
  2191. q->max = max;
  2192. /* If the user passed an items pointer, he wants a copy of
  2193. * the array. */
  2194. if (items)
  2195. num_arrays++;
  2196. q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
  2197. if (q->pool == NULL)
  2198. return -ENOMEM;
  2199. kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
  2200. for (i = 0; i < max; i++) {
  2201. q->pool[i] = kzalloc(item_size, GFP_KERNEL);
  2202. if (q->pool[i] == NULL) {
  2203. q->max = i;
  2204. goto enomem;
  2205. }
  2206. kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
  2207. }
  2208. if (items) {
  2209. *items = q->pool + max;
  2210. memcpy(*items, q->pool, max * sizeof(void *));
  2211. }
  2212. return 0;
  2213. enomem:
  2214. iscsi_pool_free(q);
  2215. return -ENOMEM;
  2216. }
  2217. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  2218. void iscsi_pool_free(struct iscsi_pool *q)
  2219. {
  2220. int i;
  2221. for (i = 0; i < q->max; i++)
  2222. kfree(q->pool[i]);
  2223. kfree(q->pool);
  2224. }
  2225. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  2226. /**
  2227. * iscsi_host_add - add host to system
  2228. * @shost: scsi host
  2229. * @pdev: parent device
  2230. *
  2231. * This should be called by partial offload and software iscsi drivers
  2232. * to add a host to the system.
  2233. */
  2234. int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
  2235. {
  2236. if (!shost->can_queue)
  2237. shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
  2238. if (!shost->cmd_per_lun)
  2239. shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
  2240. if (!shost->transportt->eh_timed_out)
  2241. shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
  2242. return scsi_add_host(shost, pdev);
  2243. }
  2244. EXPORT_SYMBOL_GPL(iscsi_host_add);
  2245. /**
  2246. * iscsi_host_alloc - allocate a host and driver data
  2247. * @sht: scsi host template
  2248. * @dd_data_size: driver host data size
  2249. * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
  2250. *
  2251. * This should be called by partial offload and software iscsi drivers.
  2252. * To access the driver specific memory use the iscsi_host_priv() macro.
  2253. */
  2254. struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
  2255. int dd_data_size, bool xmit_can_sleep)
  2256. {
  2257. struct Scsi_Host *shost;
  2258. struct iscsi_host *ihost;
  2259. shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
  2260. if (!shost)
  2261. return NULL;
  2262. ihost = shost_priv(shost);
  2263. if (xmit_can_sleep) {
  2264. snprintf(ihost->workq_name, sizeof(ihost->workq_name),
  2265. "iscsi_q_%d", shost->host_no);
  2266. ihost->workq = create_singlethread_workqueue(ihost->workq_name);
  2267. if (!ihost->workq)
  2268. goto free_host;
  2269. }
  2270. spin_lock_init(&ihost->lock);
  2271. ihost->state = ISCSI_HOST_SETUP;
  2272. ihost->num_sessions = 0;
  2273. init_waitqueue_head(&ihost->session_removal_wq);
  2274. return shost;
  2275. free_host:
  2276. scsi_host_put(shost);
  2277. return NULL;
  2278. }
  2279. EXPORT_SYMBOL_GPL(iscsi_host_alloc);
  2280. static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
  2281. {
  2282. iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
  2283. }
  2284. /**
  2285. * iscsi_host_remove - remove host and sessions
  2286. * @shost: scsi host
  2287. *
  2288. * If there are any sessions left, this will initiate the removal and wait
  2289. * for the completion.
  2290. */
  2291. void iscsi_host_remove(struct Scsi_Host *shost)
  2292. {
  2293. struct iscsi_host *ihost = shost_priv(shost);
  2294. unsigned long flags;
  2295. spin_lock_irqsave(&ihost->lock, flags);
  2296. ihost->state = ISCSI_HOST_REMOVED;
  2297. spin_unlock_irqrestore(&ihost->lock, flags);
  2298. iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
  2299. wait_event_interruptible(ihost->session_removal_wq,
  2300. ihost->num_sessions == 0);
  2301. if (signal_pending(current))
  2302. flush_signals(current);
  2303. scsi_remove_host(shost);
  2304. if (ihost->workq)
  2305. destroy_workqueue(ihost->workq);
  2306. }
  2307. EXPORT_SYMBOL_GPL(iscsi_host_remove);
  2308. void iscsi_host_free(struct Scsi_Host *shost)
  2309. {
  2310. struct iscsi_host *ihost = shost_priv(shost);
  2311. kfree(ihost->netdev);
  2312. kfree(ihost->hwaddress);
  2313. kfree(ihost->initiatorname);
  2314. scsi_host_put(shost);
  2315. }
  2316. EXPORT_SYMBOL_GPL(iscsi_host_free);
  2317. static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
  2318. {
  2319. struct iscsi_host *ihost = shost_priv(shost);
  2320. unsigned long flags;
  2321. shost = scsi_host_get(shost);
  2322. if (!shost) {
  2323. printk(KERN_ERR "Invalid state. Cannot notify host removal "
  2324. "of session teardown event because host already "
  2325. "removed.\n");
  2326. return;
  2327. }
  2328. spin_lock_irqsave(&ihost->lock, flags);
  2329. ihost->num_sessions--;
  2330. if (ihost->num_sessions == 0)
  2331. wake_up(&ihost->session_removal_wq);
  2332. spin_unlock_irqrestore(&ihost->lock, flags);
  2333. scsi_host_put(shost);
  2334. }
  2335. /**
  2336. * iscsi_session_setup - create iscsi cls session and host and session
  2337. * @iscsit: iscsi transport template
  2338. * @shost: scsi host
  2339. * @cmds_max: session can queue
  2340. * @cmd_task_size: LLD task private data size
  2341. * @initial_cmdsn: initial CmdSN
  2342. *
  2343. * This can be used by software iscsi_transports that allocate
  2344. * a session per scsi host.
  2345. *
  2346. * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
  2347. * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
  2348. * for nop handling and login/logout requests.
  2349. */
  2350. struct iscsi_cls_session *
  2351. iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
  2352. uint16_t cmds_max, int dd_size, int cmd_task_size,
  2353. uint32_t initial_cmdsn, unsigned int id)
  2354. {
  2355. struct iscsi_host *ihost = shost_priv(shost);
  2356. struct iscsi_session *session;
  2357. struct iscsi_cls_session *cls_session;
  2358. int cmd_i, scsi_cmds, total_cmds = cmds_max;
  2359. unsigned long flags;
  2360. spin_lock_irqsave(&ihost->lock, flags);
  2361. if (ihost->state == ISCSI_HOST_REMOVED) {
  2362. spin_unlock_irqrestore(&ihost->lock, flags);
  2363. return NULL;
  2364. }
  2365. ihost->num_sessions++;
  2366. spin_unlock_irqrestore(&ihost->lock, flags);
  2367. if (!total_cmds)
  2368. total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
  2369. /*
  2370. * The iscsi layer needs some tasks for nop handling and tmfs,
  2371. * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
  2372. * + 1 command for scsi IO.
  2373. */
  2374. if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
  2375. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  2376. "must be a power of two that is at least %d.\n",
  2377. total_cmds, ISCSI_TOTAL_CMDS_MIN);
  2378. goto dec_session_count;
  2379. }
  2380. if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
  2381. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  2382. "must be a power of 2 less than or equal to %d.\n",
  2383. cmds_max, ISCSI_TOTAL_CMDS_MAX);
  2384. total_cmds = ISCSI_TOTAL_CMDS_MAX;
  2385. }
  2386. if (!is_power_of_2(total_cmds)) {
  2387. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  2388. "must be a power of 2.\n", total_cmds);
  2389. total_cmds = rounddown_pow_of_two(total_cmds);
  2390. if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
  2391. return NULL;
  2392. printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
  2393. total_cmds);
  2394. }
  2395. scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
  2396. cls_session = iscsi_alloc_session(shost, iscsit,
  2397. sizeof(struct iscsi_session) +
  2398. dd_size);
  2399. if (!cls_session)
  2400. goto dec_session_count;
  2401. session = cls_session->dd_data;
  2402. session->cls_session = cls_session;
  2403. session->host = shost;
  2404. session->state = ISCSI_STATE_FREE;
  2405. session->fast_abort = 1;
  2406. session->tgt_reset_timeout = 30;
  2407. session->lu_reset_timeout = 15;
  2408. session->abort_timeout = 10;
  2409. session->scsi_cmds_max = scsi_cmds;
  2410. session->cmds_max = total_cmds;
  2411. session->queued_cmdsn = session->cmdsn = initial_cmdsn;
  2412. session->exp_cmdsn = initial_cmdsn + 1;
  2413. session->max_cmdsn = initial_cmdsn + 1;
  2414. session->max_r2t = 1;
  2415. session->tt = iscsit;
  2416. session->dd_data = cls_session->dd_data + sizeof(*session);
  2417. mutex_init(&session->eh_mutex);
  2418. spin_lock_init(&session->lock);
  2419. /* initialize SCSI PDU commands pool */
  2420. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  2421. (void***)&session->cmds,
  2422. cmd_task_size + sizeof(struct iscsi_task)))
  2423. goto cmdpool_alloc_fail;
  2424. /* pre-format cmds pool with ITT */
  2425. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  2426. struct iscsi_task *task = session->cmds[cmd_i];
  2427. if (cmd_task_size)
  2428. task->dd_data = &task[1];
  2429. task->itt = cmd_i;
  2430. task->state = ISCSI_TASK_FREE;
  2431. INIT_LIST_HEAD(&task->running);
  2432. }
  2433. if (!try_module_get(iscsit->owner))
  2434. goto module_get_fail;
  2435. if (iscsi_add_session(cls_session, id))
  2436. goto cls_session_fail;
  2437. return cls_session;
  2438. cls_session_fail:
  2439. module_put(iscsit->owner);
  2440. module_get_fail:
  2441. iscsi_pool_free(&session->cmdpool);
  2442. cmdpool_alloc_fail:
  2443. iscsi_free_session(cls_session);
  2444. dec_session_count:
  2445. iscsi_host_dec_session_cnt(shost);
  2446. return NULL;
  2447. }
  2448. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  2449. /**
  2450. * iscsi_session_teardown - destroy session, host, and cls_session
  2451. * @cls_session: iscsi session
  2452. *
  2453. * The driver must have called iscsi_remove_session before
  2454. * calling this.
  2455. */
  2456. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  2457. {
  2458. struct iscsi_session *session = cls_session->dd_data;
  2459. struct module *owner = cls_session->transport->owner;
  2460. struct Scsi_Host *shost = session->host;
  2461. iscsi_pool_free(&session->cmdpool);
  2462. kfree(session->password);
  2463. kfree(session->password_in);
  2464. kfree(session->username);
  2465. kfree(session->username_in);
  2466. kfree(session->targetname);
  2467. kfree(session->initiatorname);
  2468. kfree(session->ifacename);
  2469. iscsi_destroy_session(cls_session);
  2470. iscsi_host_dec_session_cnt(shost);
  2471. module_put(owner);
  2472. }
  2473. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  2474. /**
  2475. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  2476. * @cls_session: iscsi_cls_session
  2477. * @dd_size: private driver data size
  2478. * @conn_idx: cid
  2479. */
  2480. struct iscsi_cls_conn *
  2481. iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
  2482. uint32_t conn_idx)
  2483. {
  2484. struct iscsi_session *session = cls_session->dd_data;
  2485. struct iscsi_conn *conn;
  2486. struct iscsi_cls_conn *cls_conn;
  2487. char *data;
  2488. cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
  2489. conn_idx);
  2490. if (!cls_conn)
  2491. return NULL;
  2492. conn = cls_conn->dd_data;
  2493. memset(conn, 0, sizeof(*conn) + dd_size);
  2494. conn->dd_data = cls_conn->dd_data + sizeof(*conn);
  2495. conn->session = session;
  2496. conn->cls_conn = cls_conn;
  2497. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  2498. conn->id = conn_idx;
  2499. conn->exp_statsn = 0;
  2500. conn->tmf_state = TMF_INITIAL;
  2501. init_timer(&conn->transport_timer);
  2502. conn->transport_timer.data = (unsigned long)conn;
  2503. conn->transport_timer.function = iscsi_check_transport_timeouts;
  2504. INIT_LIST_HEAD(&conn->mgmtqueue);
  2505. INIT_LIST_HEAD(&conn->cmdqueue);
  2506. INIT_LIST_HEAD(&conn->requeue);
  2507. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  2508. /* allocate login_task used for the login/text sequences */
  2509. spin_lock_bh(&session->lock);
  2510. if (!kfifo_out(&session->cmdpool.queue,
  2511. (void*)&conn->login_task,
  2512. sizeof(void*))) {
  2513. spin_unlock_bh(&session->lock);
  2514. goto login_task_alloc_fail;
  2515. }
  2516. spin_unlock_bh(&session->lock);
  2517. data = (char *) __get_free_pages(GFP_KERNEL,
  2518. get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
  2519. if (!data)
  2520. goto login_task_data_alloc_fail;
  2521. conn->login_task->data = conn->data = data;
  2522. init_timer(&conn->tmf_timer);
  2523. init_waitqueue_head(&conn->ehwait);
  2524. return cls_conn;
  2525. login_task_data_alloc_fail:
  2526. kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
  2527. sizeof(void*));
  2528. login_task_alloc_fail:
  2529. iscsi_destroy_conn(cls_conn);
  2530. return NULL;
  2531. }
  2532. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  2533. /**
  2534. * iscsi_conn_teardown - teardown iscsi connection
  2535. * cls_conn: iscsi class connection
  2536. *
  2537. * TODO: we may need to make this into a two step process
  2538. * like scsi-mls remove + put host
  2539. */
  2540. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  2541. {
  2542. struct iscsi_conn *conn = cls_conn->dd_data;
  2543. struct iscsi_session *session = conn->session;
  2544. unsigned long flags;
  2545. del_timer_sync(&conn->transport_timer);
  2546. spin_lock_bh(&session->lock);
  2547. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  2548. if (session->leadconn == conn) {
  2549. /*
  2550. * leading connection? then give up on recovery.
  2551. */
  2552. session->state = ISCSI_STATE_TERMINATE;
  2553. wake_up(&conn->ehwait);
  2554. }
  2555. spin_unlock_bh(&session->lock);
  2556. /*
  2557. * Block until all in-progress commands for this connection
  2558. * time out or fail.
  2559. */
  2560. for (;;) {
  2561. spin_lock_irqsave(session->host->host_lock, flags);
  2562. if (!session->host->host_busy) { /* OK for ERL == 0 */
  2563. spin_unlock_irqrestore(session->host->host_lock, flags);
  2564. break;
  2565. }
  2566. spin_unlock_irqrestore(session->host->host_lock, flags);
  2567. msleep_interruptible(500);
  2568. iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
  2569. "host_busy %d host_failed %d\n",
  2570. session->host->host_busy,
  2571. session->host->host_failed);
  2572. /*
  2573. * force eh_abort() to unblock
  2574. */
  2575. wake_up(&conn->ehwait);
  2576. }
  2577. /* flush queued up work because we free the connection below */
  2578. iscsi_suspend_tx(conn);
  2579. spin_lock_bh(&session->lock);
  2580. free_pages((unsigned long) conn->data,
  2581. get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
  2582. kfree(conn->persistent_address);
  2583. kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
  2584. sizeof(void*));
  2585. if (session->leadconn == conn)
  2586. session->leadconn = NULL;
  2587. spin_unlock_bh(&session->lock);
  2588. iscsi_destroy_conn(cls_conn);
  2589. }
  2590. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  2591. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  2592. {
  2593. struct iscsi_conn *conn = cls_conn->dd_data;
  2594. struct iscsi_session *session = conn->session;
  2595. if (!session) {
  2596. iscsi_conn_printk(KERN_ERR, conn,
  2597. "can't start unbound connection\n");
  2598. return -EPERM;
  2599. }
  2600. if ((session->imm_data_en || !session->initial_r2t_en) &&
  2601. session->first_burst > session->max_burst) {
  2602. iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
  2603. "first_burst %d max_burst %d\n",
  2604. session->first_burst, session->max_burst);
  2605. return -EINVAL;
  2606. }
  2607. if (conn->ping_timeout && !conn->recv_timeout) {
  2608. iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
  2609. "zero. Using 5 seconds\n.");
  2610. conn->recv_timeout = 5;
  2611. }
  2612. if (conn->recv_timeout && !conn->ping_timeout) {
  2613. iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
  2614. "zero. Using 5 seconds.\n");
  2615. conn->ping_timeout = 5;
  2616. }
  2617. spin_lock_bh(&session->lock);
  2618. conn->c_stage = ISCSI_CONN_STARTED;
  2619. session->state = ISCSI_STATE_LOGGED_IN;
  2620. session->queued_cmdsn = session->cmdsn;
  2621. conn->last_recv = jiffies;
  2622. conn->last_ping = jiffies;
  2623. if (conn->recv_timeout && conn->ping_timeout)
  2624. mod_timer(&conn->transport_timer,
  2625. jiffies + (conn->recv_timeout * HZ));
  2626. switch(conn->stop_stage) {
  2627. case STOP_CONN_RECOVER:
  2628. /*
  2629. * unblock eh_abort() if it is blocked. re-try all
  2630. * commands after successful recovery
  2631. */
  2632. conn->stop_stage = 0;
  2633. conn->tmf_state = TMF_INITIAL;
  2634. session->age++;
  2635. if (session->age == 16)
  2636. session->age = 0;
  2637. break;
  2638. case STOP_CONN_TERM:
  2639. conn->stop_stage = 0;
  2640. break;
  2641. default:
  2642. break;
  2643. }
  2644. spin_unlock_bh(&session->lock);
  2645. iscsi_unblock_session(session->cls_session);
  2646. wake_up(&conn->ehwait);
  2647. return 0;
  2648. }
  2649. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  2650. static void
  2651. fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
  2652. {
  2653. struct iscsi_task *task;
  2654. int i, state;
  2655. for (i = 0; i < conn->session->cmds_max; i++) {
  2656. task = conn->session->cmds[i];
  2657. if (task->sc)
  2658. continue;
  2659. if (task->state == ISCSI_TASK_FREE)
  2660. continue;
  2661. ISCSI_DBG_SESSION(conn->session,
  2662. "failing mgmt itt 0x%x state %d\n",
  2663. task->itt, task->state);
  2664. state = ISCSI_TASK_ABRT_SESS_RECOV;
  2665. if (task->state == ISCSI_TASK_PENDING)
  2666. state = ISCSI_TASK_COMPLETED;
  2667. iscsi_complete_task(task, state);
  2668. }
  2669. }
  2670. static void iscsi_start_session_recovery(struct iscsi_session *session,
  2671. struct iscsi_conn *conn, int flag)
  2672. {
  2673. int old_stop_stage;
  2674. mutex_lock(&session->eh_mutex);
  2675. spin_lock_bh(&session->lock);
  2676. if (conn->stop_stage == STOP_CONN_TERM) {
  2677. spin_unlock_bh(&session->lock);
  2678. mutex_unlock(&session->eh_mutex);
  2679. return;
  2680. }
  2681. /*
  2682. * When this is called for the in_login state, we only want to clean
  2683. * up the login task and connection. We do not need to block and set
  2684. * the recovery state again
  2685. */
  2686. if (flag == STOP_CONN_TERM)
  2687. session->state = ISCSI_STATE_TERMINATE;
  2688. else if (conn->stop_stage != STOP_CONN_RECOVER)
  2689. session->state = ISCSI_STATE_IN_RECOVERY;
  2690. old_stop_stage = conn->stop_stage;
  2691. conn->stop_stage = flag;
  2692. spin_unlock_bh(&session->lock);
  2693. del_timer_sync(&conn->transport_timer);
  2694. iscsi_suspend_tx(conn);
  2695. spin_lock_bh(&session->lock);
  2696. conn->c_stage = ISCSI_CONN_STOPPED;
  2697. spin_unlock_bh(&session->lock);
  2698. /*
  2699. * for connection level recovery we should not calculate
  2700. * header digest. conn->hdr_size used for optimization
  2701. * in hdr_extract() and will be re-negotiated at
  2702. * set_param() time.
  2703. */
  2704. if (flag == STOP_CONN_RECOVER) {
  2705. conn->hdrdgst_en = 0;
  2706. conn->datadgst_en = 0;
  2707. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  2708. old_stop_stage != STOP_CONN_RECOVER) {
  2709. ISCSI_DBG_SESSION(session, "blocking session\n");
  2710. iscsi_block_session(session->cls_session);
  2711. }
  2712. }
  2713. /*
  2714. * flush queues.
  2715. */
  2716. spin_lock_bh(&session->lock);
  2717. fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
  2718. fail_mgmt_tasks(session, conn);
  2719. memset(&conn->tmhdr, 0, sizeof(conn->tmhdr));
  2720. spin_unlock_bh(&session->lock);
  2721. mutex_unlock(&session->eh_mutex);
  2722. }
  2723. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  2724. {
  2725. struct iscsi_conn *conn = cls_conn->dd_data;
  2726. struct iscsi_session *session = conn->session;
  2727. switch (flag) {
  2728. case STOP_CONN_RECOVER:
  2729. case STOP_CONN_TERM:
  2730. iscsi_start_session_recovery(session, conn, flag);
  2731. break;
  2732. default:
  2733. iscsi_conn_printk(KERN_ERR, conn,
  2734. "invalid stop flag %d\n", flag);
  2735. }
  2736. }
  2737. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  2738. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  2739. struct iscsi_cls_conn *cls_conn, int is_leading)
  2740. {
  2741. struct iscsi_session *session = cls_session->dd_data;
  2742. struct iscsi_conn *conn = cls_conn->dd_data;
  2743. spin_lock_bh(&session->lock);
  2744. if (is_leading)
  2745. session->leadconn = conn;
  2746. spin_unlock_bh(&session->lock);
  2747. /*
  2748. * Unblock xmitworker(), Login Phase will pass through.
  2749. */
  2750. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  2751. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  2752. return 0;
  2753. }
  2754. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  2755. static int iscsi_switch_str_param(char **param, char *new_val_buf)
  2756. {
  2757. char *new_val;
  2758. if (*param) {
  2759. if (!strcmp(*param, new_val_buf))
  2760. return 0;
  2761. }
  2762. new_val = kstrdup(new_val_buf, GFP_NOIO);
  2763. if (!new_val)
  2764. return -ENOMEM;
  2765. kfree(*param);
  2766. *param = new_val;
  2767. return 0;
  2768. }
  2769. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  2770. enum iscsi_param param, char *buf, int buflen)
  2771. {
  2772. struct iscsi_conn *conn = cls_conn->dd_data;
  2773. struct iscsi_session *session = conn->session;
  2774. uint32_t value;
  2775. switch(param) {
  2776. case ISCSI_PARAM_FAST_ABORT:
  2777. sscanf(buf, "%d", &session->fast_abort);
  2778. break;
  2779. case ISCSI_PARAM_ABORT_TMO:
  2780. sscanf(buf, "%d", &session->abort_timeout);
  2781. break;
  2782. case ISCSI_PARAM_LU_RESET_TMO:
  2783. sscanf(buf, "%d", &session->lu_reset_timeout);
  2784. break;
  2785. case ISCSI_PARAM_TGT_RESET_TMO:
  2786. sscanf(buf, "%d", &session->tgt_reset_timeout);
  2787. break;
  2788. case ISCSI_PARAM_PING_TMO:
  2789. sscanf(buf, "%d", &conn->ping_timeout);
  2790. break;
  2791. case ISCSI_PARAM_RECV_TMO:
  2792. sscanf(buf, "%d", &conn->recv_timeout);
  2793. break;
  2794. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  2795. sscanf(buf, "%d", &conn->max_recv_dlength);
  2796. break;
  2797. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  2798. sscanf(buf, "%d", &conn->max_xmit_dlength);
  2799. break;
  2800. case ISCSI_PARAM_HDRDGST_EN:
  2801. sscanf(buf, "%d", &conn->hdrdgst_en);
  2802. break;
  2803. case ISCSI_PARAM_DATADGST_EN:
  2804. sscanf(buf, "%d", &conn->datadgst_en);
  2805. break;
  2806. case ISCSI_PARAM_INITIAL_R2T_EN:
  2807. sscanf(buf, "%d", &session->initial_r2t_en);
  2808. break;
  2809. case ISCSI_PARAM_MAX_R2T:
  2810. sscanf(buf, "%d", &session->max_r2t);
  2811. break;
  2812. case ISCSI_PARAM_IMM_DATA_EN:
  2813. sscanf(buf, "%d", &session->imm_data_en);
  2814. break;
  2815. case ISCSI_PARAM_FIRST_BURST:
  2816. sscanf(buf, "%d", &session->first_burst);
  2817. break;
  2818. case ISCSI_PARAM_MAX_BURST:
  2819. sscanf(buf, "%d", &session->max_burst);
  2820. break;
  2821. case ISCSI_PARAM_PDU_INORDER_EN:
  2822. sscanf(buf, "%d", &session->pdu_inorder_en);
  2823. break;
  2824. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2825. sscanf(buf, "%d", &session->dataseq_inorder_en);
  2826. break;
  2827. case ISCSI_PARAM_ERL:
  2828. sscanf(buf, "%d", &session->erl);
  2829. break;
  2830. case ISCSI_PARAM_IFMARKER_EN:
  2831. sscanf(buf, "%d", &value);
  2832. BUG_ON(value);
  2833. break;
  2834. case ISCSI_PARAM_OFMARKER_EN:
  2835. sscanf(buf, "%d", &value);
  2836. BUG_ON(value);
  2837. break;
  2838. case ISCSI_PARAM_EXP_STATSN:
  2839. sscanf(buf, "%u", &conn->exp_statsn);
  2840. break;
  2841. case ISCSI_PARAM_USERNAME:
  2842. return iscsi_switch_str_param(&session->username, buf);
  2843. case ISCSI_PARAM_USERNAME_IN:
  2844. return iscsi_switch_str_param(&session->username_in, buf);
  2845. case ISCSI_PARAM_PASSWORD:
  2846. return iscsi_switch_str_param(&session->password, buf);
  2847. case ISCSI_PARAM_PASSWORD_IN:
  2848. return iscsi_switch_str_param(&session->password_in, buf);
  2849. case ISCSI_PARAM_TARGET_NAME:
  2850. return iscsi_switch_str_param(&session->targetname, buf);
  2851. case ISCSI_PARAM_TPGT:
  2852. sscanf(buf, "%d", &session->tpgt);
  2853. break;
  2854. case ISCSI_PARAM_PERSISTENT_PORT:
  2855. sscanf(buf, "%d", &conn->persistent_port);
  2856. break;
  2857. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  2858. return iscsi_switch_str_param(&conn->persistent_address, buf);
  2859. case ISCSI_PARAM_IFACE_NAME:
  2860. return iscsi_switch_str_param(&session->ifacename, buf);
  2861. case ISCSI_PARAM_INITIATOR_NAME:
  2862. return iscsi_switch_str_param(&session->initiatorname, buf);
  2863. default:
  2864. return -ENOSYS;
  2865. }
  2866. return 0;
  2867. }
  2868. EXPORT_SYMBOL_GPL(iscsi_set_param);
  2869. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  2870. enum iscsi_param param, char *buf)
  2871. {
  2872. struct iscsi_session *session = cls_session->dd_data;
  2873. int len;
  2874. switch(param) {
  2875. case ISCSI_PARAM_FAST_ABORT:
  2876. len = sprintf(buf, "%d\n", session->fast_abort);
  2877. break;
  2878. case ISCSI_PARAM_ABORT_TMO:
  2879. len = sprintf(buf, "%d\n", session->abort_timeout);
  2880. break;
  2881. case ISCSI_PARAM_LU_RESET_TMO:
  2882. len = sprintf(buf, "%d\n", session->lu_reset_timeout);
  2883. break;
  2884. case ISCSI_PARAM_TGT_RESET_TMO:
  2885. len = sprintf(buf, "%d\n", session->tgt_reset_timeout);
  2886. break;
  2887. case ISCSI_PARAM_INITIAL_R2T_EN:
  2888. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  2889. break;
  2890. case ISCSI_PARAM_MAX_R2T:
  2891. len = sprintf(buf, "%hu\n", session->max_r2t);
  2892. break;
  2893. case ISCSI_PARAM_IMM_DATA_EN:
  2894. len = sprintf(buf, "%d\n", session->imm_data_en);
  2895. break;
  2896. case ISCSI_PARAM_FIRST_BURST:
  2897. len = sprintf(buf, "%u\n", session->first_burst);
  2898. break;
  2899. case ISCSI_PARAM_MAX_BURST:
  2900. len = sprintf(buf, "%u\n", session->max_burst);
  2901. break;
  2902. case ISCSI_PARAM_PDU_INORDER_EN:
  2903. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  2904. break;
  2905. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2906. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  2907. break;
  2908. case ISCSI_PARAM_ERL:
  2909. len = sprintf(buf, "%d\n", session->erl);
  2910. break;
  2911. case ISCSI_PARAM_TARGET_NAME:
  2912. len = sprintf(buf, "%s\n", session->targetname);
  2913. break;
  2914. case ISCSI_PARAM_TPGT:
  2915. len = sprintf(buf, "%d\n", session->tpgt);
  2916. break;
  2917. case ISCSI_PARAM_USERNAME:
  2918. len = sprintf(buf, "%s\n", session->username);
  2919. break;
  2920. case ISCSI_PARAM_USERNAME_IN:
  2921. len = sprintf(buf, "%s\n", session->username_in);
  2922. break;
  2923. case ISCSI_PARAM_PASSWORD:
  2924. len = sprintf(buf, "%s\n", session->password);
  2925. break;
  2926. case ISCSI_PARAM_PASSWORD_IN:
  2927. len = sprintf(buf, "%s\n", session->password_in);
  2928. break;
  2929. case ISCSI_PARAM_IFACE_NAME:
  2930. len = sprintf(buf, "%s\n", session->ifacename);
  2931. break;
  2932. case ISCSI_PARAM_INITIATOR_NAME:
  2933. len = sprintf(buf, "%s\n", session->initiatorname);
  2934. break;
  2935. default:
  2936. return -ENOSYS;
  2937. }
  2938. return len;
  2939. }
  2940. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  2941. int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
  2942. enum iscsi_param param, char *buf)
  2943. {
  2944. struct sockaddr_in6 *sin6 = NULL;
  2945. struct sockaddr_in *sin = NULL;
  2946. int len;
  2947. switch (addr->ss_family) {
  2948. case AF_INET:
  2949. sin = (struct sockaddr_in *)addr;
  2950. break;
  2951. case AF_INET6:
  2952. sin6 = (struct sockaddr_in6 *)addr;
  2953. break;
  2954. default:
  2955. return -EINVAL;
  2956. }
  2957. switch (param) {
  2958. case ISCSI_PARAM_CONN_ADDRESS:
  2959. case ISCSI_HOST_PARAM_IPADDRESS:
  2960. if (sin)
  2961. len = sprintf(buf, "%pI4\n", &sin->sin_addr.s_addr);
  2962. else
  2963. len = sprintf(buf, "%pI6\n", &sin6->sin6_addr);
  2964. break;
  2965. case ISCSI_PARAM_CONN_PORT:
  2966. if (sin)
  2967. len = sprintf(buf, "%hu\n", be16_to_cpu(sin->sin_port));
  2968. else
  2969. len = sprintf(buf, "%hu\n",
  2970. be16_to_cpu(sin6->sin6_port));
  2971. break;
  2972. default:
  2973. return -EINVAL;
  2974. }
  2975. return len;
  2976. }
  2977. EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param);
  2978. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  2979. enum iscsi_param param, char *buf)
  2980. {
  2981. struct iscsi_conn *conn = cls_conn->dd_data;
  2982. int len;
  2983. switch(param) {
  2984. case ISCSI_PARAM_PING_TMO:
  2985. len = sprintf(buf, "%u\n", conn->ping_timeout);
  2986. break;
  2987. case ISCSI_PARAM_RECV_TMO:
  2988. len = sprintf(buf, "%u\n", conn->recv_timeout);
  2989. break;
  2990. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  2991. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  2992. break;
  2993. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  2994. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  2995. break;
  2996. case ISCSI_PARAM_HDRDGST_EN:
  2997. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  2998. break;
  2999. case ISCSI_PARAM_DATADGST_EN:
  3000. len = sprintf(buf, "%d\n", conn->datadgst_en);
  3001. break;
  3002. case ISCSI_PARAM_IFMARKER_EN:
  3003. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  3004. break;
  3005. case ISCSI_PARAM_OFMARKER_EN:
  3006. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  3007. break;
  3008. case ISCSI_PARAM_EXP_STATSN:
  3009. len = sprintf(buf, "%u\n", conn->exp_statsn);
  3010. break;
  3011. case ISCSI_PARAM_PERSISTENT_PORT:
  3012. len = sprintf(buf, "%d\n", conn->persistent_port);
  3013. break;
  3014. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  3015. len = sprintf(buf, "%s\n", conn->persistent_address);
  3016. break;
  3017. default:
  3018. return -ENOSYS;
  3019. }
  3020. return len;
  3021. }
  3022. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  3023. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  3024. char *buf)
  3025. {
  3026. struct iscsi_host *ihost = shost_priv(shost);
  3027. int len;
  3028. switch (param) {
  3029. case ISCSI_HOST_PARAM_NETDEV_NAME:
  3030. len = sprintf(buf, "%s\n", ihost->netdev);
  3031. break;
  3032. case ISCSI_HOST_PARAM_HWADDRESS:
  3033. len = sprintf(buf, "%s\n", ihost->hwaddress);
  3034. break;
  3035. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  3036. len = sprintf(buf, "%s\n", ihost->initiatorname);
  3037. break;
  3038. default:
  3039. return -ENOSYS;
  3040. }
  3041. return len;
  3042. }
  3043. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  3044. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  3045. char *buf, int buflen)
  3046. {
  3047. struct iscsi_host *ihost = shost_priv(shost);
  3048. switch (param) {
  3049. case ISCSI_HOST_PARAM_NETDEV_NAME:
  3050. return iscsi_switch_str_param(&ihost->netdev, buf);
  3051. case ISCSI_HOST_PARAM_HWADDRESS:
  3052. return iscsi_switch_str_param(&ihost->hwaddress, buf);
  3053. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  3054. return iscsi_switch_str_param(&ihost->initiatorname, buf);
  3055. default:
  3056. return -ENOSYS;
  3057. }
  3058. return 0;
  3059. }
  3060. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  3061. MODULE_AUTHOR("Mike Christie");
  3062. MODULE_DESCRIPTION("iSCSI library functions");
  3063. MODULE_LICENSE("GPL");