libiscsi.c 98 KB

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