libiscsi.c 98 KB

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