libiscsi.c 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651
  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. case ISCSI_STATE_IN_RECOVERY:
  1498. reason = FAILURE_SESSION_IN_RECOVERY;
  1499. sc->result = DID_IMM_RETRY << 16;
  1500. break;
  1501. case ISCSI_STATE_LOGGING_OUT:
  1502. reason = FAILURE_SESSION_LOGGING_OUT;
  1503. sc->result = DID_IMM_RETRY << 16;
  1504. break;
  1505. case ISCSI_STATE_RECOVERY_FAILED:
  1506. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  1507. sc->result = DID_TRANSPORT_FAILFAST << 16;
  1508. break;
  1509. case ISCSI_STATE_TERMINATE:
  1510. reason = FAILURE_SESSION_TERMINATE;
  1511. sc->result = DID_NO_CONNECT << 16;
  1512. break;
  1513. default:
  1514. reason = FAILURE_SESSION_FREED;
  1515. sc->result = DID_NO_CONNECT << 16;
  1516. }
  1517. goto fault;
  1518. }
  1519. conn = session->leadconn;
  1520. if (!conn) {
  1521. reason = FAILURE_SESSION_FREED;
  1522. sc->result = DID_NO_CONNECT << 16;
  1523. goto fault;
  1524. }
  1525. if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
  1526. reason = FAILURE_SESSION_IN_RECOVERY;
  1527. sc->result = DID_REQUEUE;
  1528. goto fault;
  1529. }
  1530. if (iscsi_check_cmdsn_window_closed(conn)) {
  1531. reason = FAILURE_WINDOW_CLOSED;
  1532. goto reject;
  1533. }
  1534. task = iscsi_alloc_task(conn, sc);
  1535. if (!task) {
  1536. reason = FAILURE_OOM;
  1537. goto reject;
  1538. }
  1539. if (!ihost->workq) {
  1540. reason = iscsi_prep_scsi_cmd_pdu(task);
  1541. if (reason) {
  1542. if (reason == -ENOMEM || reason == -EACCES) {
  1543. reason = FAILURE_OOM;
  1544. goto prepd_reject;
  1545. } else {
  1546. sc->result = DID_ABORT << 16;
  1547. goto prepd_fault;
  1548. }
  1549. }
  1550. if (session->tt->xmit_task(task)) {
  1551. session->cmdsn--;
  1552. reason = FAILURE_SESSION_NOT_READY;
  1553. goto prepd_reject;
  1554. }
  1555. } else {
  1556. spin_lock_bh(&conn->taskqueuelock);
  1557. list_add_tail(&task->running, &conn->cmdqueue);
  1558. spin_unlock_bh(&conn->taskqueuelock);
  1559. iscsi_conn_queue_work(conn);
  1560. }
  1561. session->queued_cmdsn++;
  1562. spin_unlock_bh(&session->frwd_lock);
  1563. return 0;
  1564. prepd_reject:
  1565. iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
  1566. reject:
  1567. spin_unlock_bh(&session->frwd_lock);
  1568. ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
  1569. sc->cmnd[0], reason);
  1570. return SCSI_MLQUEUE_TARGET_BUSY;
  1571. prepd_fault:
  1572. iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
  1573. fault:
  1574. spin_unlock_bh(&session->frwd_lock);
  1575. ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
  1576. sc->cmnd[0], reason);
  1577. if (!scsi_bidi_cmnd(sc))
  1578. scsi_set_resid(sc, scsi_bufflen(sc));
  1579. else {
  1580. scsi_out(sc)->resid = scsi_out(sc)->length;
  1581. scsi_in(sc)->resid = scsi_in(sc)->length;
  1582. }
  1583. sc->scsi_done(sc);
  1584. return 0;
  1585. }
  1586. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  1587. int iscsi_target_alloc(struct scsi_target *starget)
  1588. {
  1589. struct iscsi_cls_session *cls_session = starget_to_session(starget);
  1590. struct iscsi_session *session = cls_session->dd_data;
  1591. starget->can_queue = session->scsi_cmds_max;
  1592. return 0;
  1593. }
  1594. EXPORT_SYMBOL_GPL(iscsi_target_alloc);
  1595. static void iscsi_tmf_timedout(unsigned long data)
  1596. {
  1597. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1598. struct iscsi_session *session = conn->session;
  1599. spin_lock(&session->frwd_lock);
  1600. if (conn->tmf_state == TMF_QUEUED) {
  1601. conn->tmf_state = TMF_TIMEDOUT;
  1602. ISCSI_DBG_EH(session, "tmf timedout\n");
  1603. /* unblock eh_abort() */
  1604. wake_up(&conn->ehwait);
  1605. }
  1606. spin_unlock(&session->frwd_lock);
  1607. }
  1608. static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
  1609. struct iscsi_tm *hdr, int age,
  1610. int timeout)
  1611. {
  1612. struct iscsi_session *session = conn->session;
  1613. struct iscsi_task *task;
  1614. task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
  1615. NULL, 0);
  1616. if (!task) {
  1617. spin_unlock_bh(&session->frwd_lock);
  1618. iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
  1619. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1620. spin_lock_bh(&session->frwd_lock);
  1621. return -EPERM;
  1622. }
  1623. conn->tmfcmd_pdus_cnt++;
  1624. conn->tmf_timer.expires = timeout * HZ + jiffies;
  1625. conn->tmf_timer.function = iscsi_tmf_timedout;
  1626. conn->tmf_timer.data = (unsigned long)conn;
  1627. add_timer(&conn->tmf_timer);
  1628. ISCSI_DBG_EH(session, "tmf set timeout\n");
  1629. spin_unlock_bh(&session->frwd_lock);
  1630. mutex_unlock(&session->eh_mutex);
  1631. /*
  1632. * block eh thread until:
  1633. *
  1634. * 1) tmf response
  1635. * 2) tmf timeout
  1636. * 3) session is terminated or restarted or userspace has
  1637. * given up on recovery
  1638. */
  1639. wait_event_interruptible(conn->ehwait, age != session->age ||
  1640. session->state != ISCSI_STATE_LOGGED_IN ||
  1641. conn->tmf_state != TMF_QUEUED);
  1642. if (signal_pending(current))
  1643. flush_signals(current);
  1644. del_timer_sync(&conn->tmf_timer);
  1645. mutex_lock(&session->eh_mutex);
  1646. spin_lock_bh(&session->frwd_lock);
  1647. /* if the session drops it will clean up the task */
  1648. if (age != session->age ||
  1649. session->state != ISCSI_STATE_LOGGED_IN)
  1650. return -ENOTCONN;
  1651. return 0;
  1652. }
  1653. /*
  1654. * Fail commands. session lock held and recv side suspended and xmit
  1655. * thread flushed
  1656. */
  1657. static void fail_scsi_tasks(struct iscsi_conn *conn, u64 lun, int error)
  1658. {
  1659. struct iscsi_task *task;
  1660. int i;
  1661. for (i = 0; i < conn->session->cmds_max; i++) {
  1662. task = conn->session->cmds[i];
  1663. if (!task->sc || task->state == ISCSI_TASK_FREE)
  1664. continue;
  1665. if (lun != -1 && lun != task->sc->device->lun)
  1666. continue;
  1667. ISCSI_DBG_SESSION(conn->session,
  1668. "failing sc %p itt 0x%x state %d\n",
  1669. task->sc, task->itt, task->state);
  1670. fail_scsi_task(task, error);
  1671. }
  1672. }
  1673. /**
  1674. * iscsi_suspend_queue - suspend iscsi_queuecommand
  1675. * @conn: iscsi conn to stop queueing IO on
  1676. *
  1677. * This grabs the session frwd_lock to make sure no one is in
  1678. * xmit_task/queuecommand, and then sets suspend to prevent
  1679. * new commands from being queued. This only needs to be called
  1680. * by offload drivers that need to sync a path like ep disconnect
  1681. * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
  1682. * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
  1683. */
  1684. void iscsi_suspend_queue(struct iscsi_conn *conn)
  1685. {
  1686. spin_lock_bh(&conn->session->frwd_lock);
  1687. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1688. spin_unlock_bh(&conn->session->frwd_lock);
  1689. }
  1690. EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
  1691. /**
  1692. * iscsi_suspend_tx - suspend iscsi_data_xmit
  1693. * @conn: iscsi conn tp stop processing IO on.
  1694. *
  1695. * This function sets the suspend bit to prevent iscsi_data_xmit
  1696. * from sending new IO, and if work is queued on the xmit thread
  1697. * it will wait for it to be completed.
  1698. */
  1699. void iscsi_suspend_tx(struct iscsi_conn *conn)
  1700. {
  1701. struct Scsi_Host *shost = conn->session->host;
  1702. struct iscsi_host *ihost = shost_priv(shost);
  1703. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1704. if (ihost->workq)
  1705. flush_workqueue(ihost->workq);
  1706. }
  1707. EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
  1708. static void iscsi_start_tx(struct iscsi_conn *conn)
  1709. {
  1710. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1711. iscsi_conn_queue_work(conn);
  1712. }
  1713. /*
  1714. * We want to make sure a ping is in flight. It has timed out.
  1715. * And we are not busy processing a pdu that is making
  1716. * progress but got started before the ping and is taking a while
  1717. * to complete so the ping is just stuck behind it in a queue.
  1718. */
  1719. static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
  1720. {
  1721. if (conn->ping_task &&
  1722. time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
  1723. (conn->ping_timeout * HZ), jiffies))
  1724. return 1;
  1725. else
  1726. return 0;
  1727. }
  1728. enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
  1729. {
  1730. enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
  1731. struct iscsi_task *task = NULL, *running_task;
  1732. struct iscsi_cls_session *cls_session;
  1733. struct iscsi_session *session;
  1734. struct iscsi_conn *conn;
  1735. int i;
  1736. cls_session = starget_to_session(scsi_target(sc->device));
  1737. session = cls_session->dd_data;
  1738. ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
  1739. spin_lock(&session->frwd_lock);
  1740. task = (struct iscsi_task *)sc->SCp.ptr;
  1741. if (!task) {
  1742. /*
  1743. * Raced with completion. Blk layer has taken ownership
  1744. * so let timeout code complete it now.
  1745. */
  1746. rc = BLK_EH_HANDLED;
  1747. goto done;
  1748. }
  1749. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1750. /*
  1751. * We are probably in the middle of iscsi recovery so let
  1752. * that complete and handle the error.
  1753. */
  1754. rc = BLK_EH_RESET_TIMER;
  1755. goto done;
  1756. }
  1757. conn = session->leadconn;
  1758. if (!conn) {
  1759. /* In the middle of shuting down */
  1760. rc = BLK_EH_RESET_TIMER;
  1761. goto done;
  1762. }
  1763. /*
  1764. * If we have sent (at least queued to the network layer) a pdu or
  1765. * recvd one for the task since the last timeout ask for
  1766. * more time. If on the next timeout we have not made progress
  1767. * we can check if it is the task or connection when we send the
  1768. * nop as a ping.
  1769. */
  1770. if (time_after(task->last_xfer, task->last_timeout)) {
  1771. ISCSI_DBG_EH(session, "Command making progress. Asking "
  1772. "scsi-ml for more time to complete. "
  1773. "Last data xfer at %lu. Last timeout was at "
  1774. "%lu\n.", task->last_xfer, task->last_timeout);
  1775. task->have_checked_conn = false;
  1776. rc = BLK_EH_RESET_TIMER;
  1777. goto done;
  1778. }
  1779. if (!conn->recv_timeout && !conn->ping_timeout)
  1780. goto done;
  1781. /*
  1782. * if the ping timedout then we are in the middle of cleaning up
  1783. * and can let the iscsi eh handle it
  1784. */
  1785. if (iscsi_has_ping_timed_out(conn)) {
  1786. rc = BLK_EH_RESET_TIMER;
  1787. goto done;
  1788. }
  1789. for (i = 0; i < conn->session->cmds_max; i++) {
  1790. running_task = conn->session->cmds[i];
  1791. if (!running_task->sc || running_task == task ||
  1792. running_task->state != ISCSI_TASK_RUNNING)
  1793. continue;
  1794. /*
  1795. * Only check if cmds started before this one have made
  1796. * progress, or this could never fail
  1797. */
  1798. if (time_after(running_task->sc->jiffies_at_alloc,
  1799. task->sc->jiffies_at_alloc))
  1800. continue;
  1801. if (time_after(running_task->last_xfer, task->last_timeout)) {
  1802. /*
  1803. * This task has not made progress, but a task
  1804. * started before us has transferred data since
  1805. * we started/last-checked. We could be queueing
  1806. * too many tasks or the LU is bad.
  1807. *
  1808. * If the device is bad the cmds ahead of us on
  1809. * other devs will complete, and this loop will
  1810. * eventually fail starting the scsi eh.
  1811. */
  1812. ISCSI_DBG_EH(session, "Command has not made progress "
  1813. "but commands ahead of it have. "
  1814. "Asking scsi-ml for more time to "
  1815. "complete. Our last xfer vs running task "
  1816. "last xfer %lu/%lu. Last check %lu.\n",
  1817. task->last_xfer, running_task->last_xfer,
  1818. task->last_timeout);
  1819. rc = BLK_EH_RESET_TIMER;
  1820. goto done;
  1821. }
  1822. }
  1823. /* Assumes nop timeout is shorter than scsi cmd timeout */
  1824. if (task->have_checked_conn)
  1825. goto done;
  1826. /*
  1827. * Checking the transport already or nop from a cmd timeout still
  1828. * running
  1829. */
  1830. if (conn->ping_task) {
  1831. task->have_checked_conn = true;
  1832. rc = BLK_EH_RESET_TIMER;
  1833. goto done;
  1834. }
  1835. /* Make sure there is a transport check done */
  1836. iscsi_send_nopout(conn, NULL);
  1837. task->have_checked_conn = true;
  1838. rc = BLK_EH_RESET_TIMER;
  1839. done:
  1840. if (task)
  1841. task->last_timeout = jiffies;
  1842. spin_unlock(&session->frwd_lock);
  1843. ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
  1844. "timer reset" : "nh");
  1845. return rc;
  1846. }
  1847. EXPORT_SYMBOL_GPL(iscsi_eh_cmd_timed_out);
  1848. static void iscsi_check_transport_timeouts(unsigned long data)
  1849. {
  1850. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1851. struct iscsi_session *session = conn->session;
  1852. unsigned long recv_timeout, next_timeout = 0, last_recv;
  1853. spin_lock(&session->frwd_lock);
  1854. if (session->state != ISCSI_STATE_LOGGED_IN)
  1855. goto done;
  1856. recv_timeout = conn->recv_timeout;
  1857. if (!recv_timeout)
  1858. goto done;
  1859. recv_timeout *= HZ;
  1860. last_recv = conn->last_recv;
  1861. if (iscsi_has_ping_timed_out(conn)) {
  1862. iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
  1863. "expired, recv timeout %d, last rx %lu, "
  1864. "last ping %lu, now %lu\n",
  1865. conn->ping_timeout, conn->recv_timeout,
  1866. last_recv, conn->last_ping, jiffies);
  1867. spin_unlock(&session->frwd_lock);
  1868. iscsi_conn_failure(conn, ISCSI_ERR_NOP_TIMEDOUT);
  1869. return;
  1870. }
  1871. if (time_before_eq(last_recv + recv_timeout, jiffies)) {
  1872. /* send a ping to try to provoke some traffic */
  1873. ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
  1874. if (iscsi_send_nopout(conn, NULL))
  1875. next_timeout = jiffies + (1 * HZ);
  1876. else
  1877. next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
  1878. } else
  1879. next_timeout = last_recv + recv_timeout;
  1880. ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
  1881. mod_timer(&conn->transport_timer, next_timeout);
  1882. done:
  1883. spin_unlock(&session->frwd_lock);
  1884. }
  1885. static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
  1886. struct iscsi_tm *hdr)
  1887. {
  1888. memset(hdr, 0, sizeof(*hdr));
  1889. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1890. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
  1891. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1892. hdr->lun = task->lun;
  1893. hdr->rtt = task->hdr_itt;
  1894. hdr->refcmdsn = task->cmdsn;
  1895. }
  1896. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1897. {
  1898. struct iscsi_cls_session *cls_session;
  1899. struct iscsi_session *session;
  1900. struct iscsi_conn *conn;
  1901. struct iscsi_task *task;
  1902. struct iscsi_tm *hdr;
  1903. int age;
  1904. cls_session = starget_to_session(scsi_target(sc->device));
  1905. session = cls_session->dd_data;
  1906. ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
  1907. mutex_lock(&session->eh_mutex);
  1908. spin_lock_bh(&session->frwd_lock);
  1909. /*
  1910. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1911. * got the command.
  1912. */
  1913. if (!sc->SCp.ptr) {
  1914. ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
  1915. "it completed.\n");
  1916. spin_unlock_bh(&session->frwd_lock);
  1917. mutex_unlock(&session->eh_mutex);
  1918. return SUCCESS;
  1919. }
  1920. /*
  1921. * If we are not logged in or we have started a new session
  1922. * then let the host reset code handle this
  1923. */
  1924. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
  1925. sc->SCp.phase != session->age) {
  1926. spin_unlock_bh(&session->frwd_lock);
  1927. mutex_unlock(&session->eh_mutex);
  1928. ISCSI_DBG_EH(session, "failing abort due to dropped "
  1929. "session.\n");
  1930. return FAILED;
  1931. }
  1932. conn = session->leadconn;
  1933. conn->eh_abort_cnt++;
  1934. age = session->age;
  1935. task = (struct iscsi_task *)sc->SCp.ptr;
  1936. ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
  1937. sc, task->itt);
  1938. /* task completed before time out */
  1939. if (!task->sc) {
  1940. ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
  1941. goto success;
  1942. }
  1943. if (task->state == ISCSI_TASK_PENDING) {
  1944. fail_scsi_task(task, DID_ABORT);
  1945. goto success;
  1946. }
  1947. /* only have one tmf outstanding at a time */
  1948. if (conn->tmf_state != TMF_INITIAL)
  1949. goto failed;
  1950. conn->tmf_state = TMF_QUEUED;
  1951. hdr = &conn->tmhdr;
  1952. iscsi_prep_abort_task_pdu(task, hdr);
  1953. if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout))
  1954. goto failed;
  1955. switch (conn->tmf_state) {
  1956. case TMF_SUCCESS:
  1957. spin_unlock_bh(&session->frwd_lock);
  1958. /*
  1959. * stop tx side incase the target had sent a abort rsp but
  1960. * the initiator was still writing out data.
  1961. */
  1962. iscsi_suspend_tx(conn);
  1963. /*
  1964. * we do not stop the recv side because targets have been
  1965. * good and have never sent us a successful tmf response
  1966. * then sent more data for the cmd.
  1967. */
  1968. spin_lock_bh(&session->frwd_lock);
  1969. fail_scsi_task(task, DID_ABORT);
  1970. conn->tmf_state = TMF_INITIAL;
  1971. memset(hdr, 0, sizeof(*hdr));
  1972. spin_unlock_bh(&session->frwd_lock);
  1973. iscsi_start_tx(conn);
  1974. goto success_unlocked;
  1975. case TMF_TIMEDOUT:
  1976. spin_unlock_bh(&session->frwd_lock);
  1977. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  1978. goto failed_unlocked;
  1979. case TMF_NOT_FOUND:
  1980. if (!sc->SCp.ptr) {
  1981. conn->tmf_state = TMF_INITIAL;
  1982. memset(hdr, 0, sizeof(*hdr));
  1983. /* task completed before tmf abort response */
  1984. ISCSI_DBG_EH(session, "sc completed while abort in "
  1985. "progress\n");
  1986. goto success;
  1987. }
  1988. /* fall through */
  1989. default:
  1990. conn->tmf_state = TMF_INITIAL;
  1991. goto failed;
  1992. }
  1993. success:
  1994. spin_unlock_bh(&session->frwd_lock);
  1995. success_unlocked:
  1996. ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
  1997. sc, task->itt);
  1998. mutex_unlock(&session->eh_mutex);
  1999. return SUCCESS;
  2000. failed:
  2001. spin_unlock_bh(&session->frwd_lock);
  2002. failed_unlocked:
  2003. ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
  2004. task ? task->itt : 0);
  2005. mutex_unlock(&session->eh_mutex);
  2006. return FAILED;
  2007. }
  2008. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  2009. static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  2010. {
  2011. memset(hdr, 0, sizeof(*hdr));
  2012. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  2013. hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  2014. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  2015. int_to_scsilun(sc->device->lun, &hdr->lun);
  2016. hdr->rtt = RESERVED_ITT;
  2017. }
  2018. int iscsi_eh_device_reset(struct scsi_cmnd *sc)
  2019. {
  2020. struct iscsi_cls_session *cls_session;
  2021. struct iscsi_session *session;
  2022. struct iscsi_conn *conn;
  2023. struct iscsi_tm *hdr;
  2024. int rc = FAILED;
  2025. cls_session = starget_to_session(scsi_target(sc->device));
  2026. session = cls_session->dd_data;
  2027. ISCSI_DBG_EH(session, "LU Reset [sc %p lun %llu]\n", sc,
  2028. sc->device->lun);
  2029. mutex_lock(&session->eh_mutex);
  2030. spin_lock_bh(&session->frwd_lock);
  2031. /*
  2032. * Just check if we are not logged in. We cannot check for
  2033. * the phase because the reset could come from a ioctl.
  2034. */
  2035. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  2036. goto unlock;
  2037. conn = session->leadconn;
  2038. /* only have one tmf outstanding at a time */
  2039. if (conn->tmf_state != TMF_INITIAL)
  2040. goto unlock;
  2041. conn->tmf_state = TMF_QUEUED;
  2042. hdr = &conn->tmhdr;
  2043. iscsi_prep_lun_reset_pdu(sc, hdr);
  2044. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
  2045. session->lu_reset_timeout)) {
  2046. rc = FAILED;
  2047. goto unlock;
  2048. }
  2049. switch (conn->tmf_state) {
  2050. case TMF_SUCCESS:
  2051. break;
  2052. case TMF_TIMEDOUT:
  2053. spin_unlock_bh(&session->frwd_lock);
  2054. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  2055. goto done;
  2056. default:
  2057. conn->tmf_state = TMF_INITIAL;
  2058. goto unlock;
  2059. }
  2060. rc = SUCCESS;
  2061. spin_unlock_bh(&session->frwd_lock);
  2062. iscsi_suspend_tx(conn);
  2063. spin_lock_bh(&session->frwd_lock);
  2064. memset(hdr, 0, sizeof(*hdr));
  2065. fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
  2066. conn->tmf_state = TMF_INITIAL;
  2067. spin_unlock_bh(&session->frwd_lock);
  2068. iscsi_start_tx(conn);
  2069. goto done;
  2070. unlock:
  2071. spin_unlock_bh(&session->frwd_lock);
  2072. done:
  2073. ISCSI_DBG_EH(session, "dev reset result = %s\n",
  2074. rc == SUCCESS ? "SUCCESS" : "FAILED");
  2075. mutex_unlock(&session->eh_mutex);
  2076. return rc;
  2077. }
  2078. EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
  2079. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  2080. {
  2081. struct iscsi_session *session = cls_session->dd_data;
  2082. spin_lock_bh(&session->frwd_lock);
  2083. if (session->state != ISCSI_STATE_LOGGED_IN) {
  2084. session->state = ISCSI_STATE_RECOVERY_FAILED;
  2085. if (session->leadconn)
  2086. wake_up(&session->leadconn->ehwait);
  2087. }
  2088. spin_unlock_bh(&session->frwd_lock);
  2089. }
  2090. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  2091. /**
  2092. * iscsi_eh_session_reset - drop session and attempt relogin
  2093. * @sc: scsi command
  2094. *
  2095. * This function will wait for a relogin, session termination from
  2096. * userspace, or a recovery/replacement timeout.
  2097. */
  2098. int iscsi_eh_session_reset(struct scsi_cmnd *sc)
  2099. {
  2100. struct iscsi_cls_session *cls_session;
  2101. struct iscsi_session *session;
  2102. struct iscsi_conn *conn;
  2103. cls_session = starget_to_session(scsi_target(sc->device));
  2104. session = cls_session->dd_data;
  2105. conn = session->leadconn;
  2106. mutex_lock(&session->eh_mutex);
  2107. spin_lock_bh(&session->frwd_lock);
  2108. if (session->state == ISCSI_STATE_TERMINATE) {
  2109. failed:
  2110. ISCSI_DBG_EH(session,
  2111. "failing session reset: Could not log back into "
  2112. "%s, %s [age %d]\n", session->targetname,
  2113. conn->persistent_address, session->age);
  2114. spin_unlock_bh(&session->frwd_lock);
  2115. mutex_unlock(&session->eh_mutex);
  2116. return FAILED;
  2117. }
  2118. spin_unlock_bh(&session->frwd_lock);
  2119. mutex_unlock(&session->eh_mutex);
  2120. /*
  2121. * we drop the lock here but the leadconn cannot be destoyed while
  2122. * we are in the scsi eh
  2123. */
  2124. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  2125. ISCSI_DBG_EH(session, "wait for relogin\n");
  2126. wait_event_interruptible(conn->ehwait,
  2127. session->state == ISCSI_STATE_TERMINATE ||
  2128. session->state == ISCSI_STATE_LOGGED_IN ||
  2129. session->state == ISCSI_STATE_RECOVERY_FAILED);
  2130. if (signal_pending(current))
  2131. flush_signals(current);
  2132. mutex_lock(&session->eh_mutex);
  2133. spin_lock_bh(&session->frwd_lock);
  2134. if (session->state == ISCSI_STATE_LOGGED_IN) {
  2135. ISCSI_DBG_EH(session,
  2136. "session reset succeeded for %s,%s\n",
  2137. session->targetname, conn->persistent_address);
  2138. } else
  2139. goto failed;
  2140. spin_unlock_bh(&session->frwd_lock);
  2141. mutex_unlock(&session->eh_mutex);
  2142. return SUCCESS;
  2143. }
  2144. EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
  2145. static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  2146. {
  2147. memset(hdr, 0, sizeof(*hdr));
  2148. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  2149. hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  2150. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  2151. hdr->rtt = RESERVED_ITT;
  2152. }
  2153. /**
  2154. * iscsi_eh_target_reset - reset target
  2155. * @sc: scsi command
  2156. *
  2157. * This will attempt to send a warm target reset.
  2158. */
  2159. static int iscsi_eh_target_reset(struct scsi_cmnd *sc)
  2160. {
  2161. struct iscsi_cls_session *cls_session;
  2162. struct iscsi_session *session;
  2163. struct iscsi_conn *conn;
  2164. struct iscsi_tm *hdr;
  2165. int rc = FAILED;
  2166. cls_session = starget_to_session(scsi_target(sc->device));
  2167. session = cls_session->dd_data;
  2168. ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
  2169. session->targetname);
  2170. mutex_lock(&session->eh_mutex);
  2171. spin_lock_bh(&session->frwd_lock);
  2172. /*
  2173. * Just check if we are not logged in. We cannot check for
  2174. * the phase because the reset could come from a ioctl.
  2175. */
  2176. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  2177. goto unlock;
  2178. conn = session->leadconn;
  2179. /* only have one tmf outstanding at a time */
  2180. if (conn->tmf_state != TMF_INITIAL)
  2181. goto unlock;
  2182. conn->tmf_state = TMF_QUEUED;
  2183. hdr = &conn->tmhdr;
  2184. iscsi_prep_tgt_reset_pdu(sc, hdr);
  2185. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
  2186. session->tgt_reset_timeout)) {
  2187. rc = FAILED;
  2188. goto unlock;
  2189. }
  2190. switch (conn->tmf_state) {
  2191. case TMF_SUCCESS:
  2192. break;
  2193. case TMF_TIMEDOUT:
  2194. spin_unlock_bh(&session->frwd_lock);
  2195. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  2196. goto done;
  2197. default:
  2198. conn->tmf_state = TMF_INITIAL;
  2199. goto unlock;
  2200. }
  2201. rc = SUCCESS;
  2202. spin_unlock_bh(&session->frwd_lock);
  2203. iscsi_suspend_tx(conn);
  2204. spin_lock_bh(&session->frwd_lock);
  2205. memset(hdr, 0, sizeof(*hdr));
  2206. fail_scsi_tasks(conn, -1, DID_ERROR);
  2207. conn->tmf_state = TMF_INITIAL;
  2208. spin_unlock_bh(&session->frwd_lock);
  2209. iscsi_start_tx(conn);
  2210. goto done;
  2211. unlock:
  2212. spin_unlock_bh(&session->frwd_lock);
  2213. done:
  2214. ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
  2215. rc == SUCCESS ? "SUCCESS" : "FAILED");
  2216. mutex_unlock(&session->eh_mutex);
  2217. return rc;
  2218. }
  2219. /**
  2220. * iscsi_eh_recover_target - reset target and possibly the session
  2221. * @sc: scsi command
  2222. *
  2223. * This will attempt to send a warm target reset. If that fails,
  2224. * we will escalate to ERL0 session recovery.
  2225. */
  2226. int iscsi_eh_recover_target(struct scsi_cmnd *sc)
  2227. {
  2228. int rc;
  2229. rc = iscsi_eh_target_reset(sc);
  2230. if (rc == FAILED)
  2231. rc = iscsi_eh_session_reset(sc);
  2232. return rc;
  2233. }
  2234. EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
  2235. /*
  2236. * Pre-allocate a pool of @max items of @item_size. By default, the pool
  2237. * should be accessed via kfifo_{get,put} on q->queue.
  2238. * Optionally, the caller can obtain the array of object pointers
  2239. * by passing in a non-NULL @items pointer
  2240. */
  2241. int
  2242. iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
  2243. {
  2244. int i, num_arrays = 1;
  2245. memset(q, 0, sizeof(*q));
  2246. q->max = max;
  2247. /* If the user passed an items pointer, he wants a copy of
  2248. * the array. */
  2249. if (items)
  2250. num_arrays++;
  2251. q->pool = kvzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
  2252. if (q->pool == NULL)
  2253. return -ENOMEM;
  2254. kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
  2255. for (i = 0; i < max; i++) {
  2256. q->pool[i] = kzalloc(item_size, GFP_KERNEL);
  2257. if (q->pool[i] == NULL) {
  2258. q->max = i;
  2259. goto enomem;
  2260. }
  2261. kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
  2262. }
  2263. if (items) {
  2264. *items = q->pool + max;
  2265. memcpy(*items, q->pool, max * sizeof(void *));
  2266. }
  2267. return 0;
  2268. enomem:
  2269. iscsi_pool_free(q);
  2270. return -ENOMEM;
  2271. }
  2272. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  2273. void iscsi_pool_free(struct iscsi_pool *q)
  2274. {
  2275. int i;
  2276. for (i = 0; i < q->max; i++)
  2277. kfree(q->pool[i]);
  2278. kvfree(q->pool);
  2279. }
  2280. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  2281. /**
  2282. * iscsi_host_add - add host to system
  2283. * @shost: scsi host
  2284. * @pdev: parent device
  2285. *
  2286. * This should be called by partial offload and software iscsi drivers
  2287. * to add a host to the system.
  2288. */
  2289. int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
  2290. {
  2291. if (!shost->can_queue)
  2292. shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
  2293. if (!shost->cmd_per_lun)
  2294. shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
  2295. return scsi_add_host(shost, pdev);
  2296. }
  2297. EXPORT_SYMBOL_GPL(iscsi_host_add);
  2298. /**
  2299. * iscsi_host_alloc - allocate a host and driver data
  2300. * @sht: scsi host template
  2301. * @dd_data_size: driver host data size
  2302. * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
  2303. *
  2304. * This should be called by partial offload and software iscsi drivers.
  2305. * To access the driver specific memory use the iscsi_host_priv() macro.
  2306. */
  2307. struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
  2308. int dd_data_size, bool xmit_can_sleep)
  2309. {
  2310. struct Scsi_Host *shost;
  2311. struct iscsi_host *ihost;
  2312. shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
  2313. if (!shost)
  2314. return NULL;
  2315. ihost = shost_priv(shost);
  2316. if (xmit_can_sleep) {
  2317. snprintf(ihost->workq_name, sizeof(ihost->workq_name),
  2318. "iscsi_q_%d", shost->host_no);
  2319. ihost->workq = create_singlethread_workqueue(ihost->workq_name);
  2320. if (!ihost->workq)
  2321. goto free_host;
  2322. }
  2323. spin_lock_init(&ihost->lock);
  2324. ihost->state = ISCSI_HOST_SETUP;
  2325. ihost->num_sessions = 0;
  2326. init_waitqueue_head(&ihost->session_removal_wq);
  2327. return shost;
  2328. free_host:
  2329. scsi_host_put(shost);
  2330. return NULL;
  2331. }
  2332. EXPORT_SYMBOL_GPL(iscsi_host_alloc);
  2333. static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
  2334. {
  2335. iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
  2336. }
  2337. /**
  2338. * iscsi_host_remove - remove host and sessions
  2339. * @shost: scsi host
  2340. *
  2341. * If there are any sessions left, this will initiate the removal and wait
  2342. * for the completion.
  2343. */
  2344. void iscsi_host_remove(struct Scsi_Host *shost)
  2345. {
  2346. struct iscsi_host *ihost = shost_priv(shost);
  2347. unsigned long flags;
  2348. spin_lock_irqsave(&ihost->lock, flags);
  2349. ihost->state = ISCSI_HOST_REMOVED;
  2350. spin_unlock_irqrestore(&ihost->lock, flags);
  2351. iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
  2352. wait_event_interruptible(ihost->session_removal_wq,
  2353. ihost->num_sessions == 0);
  2354. if (signal_pending(current))
  2355. flush_signals(current);
  2356. scsi_remove_host(shost);
  2357. if (ihost->workq)
  2358. destroy_workqueue(ihost->workq);
  2359. }
  2360. EXPORT_SYMBOL_GPL(iscsi_host_remove);
  2361. void iscsi_host_free(struct Scsi_Host *shost)
  2362. {
  2363. struct iscsi_host *ihost = shost_priv(shost);
  2364. kfree(ihost->netdev);
  2365. kfree(ihost->hwaddress);
  2366. kfree(ihost->initiatorname);
  2367. scsi_host_put(shost);
  2368. }
  2369. EXPORT_SYMBOL_GPL(iscsi_host_free);
  2370. static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
  2371. {
  2372. struct iscsi_host *ihost = shost_priv(shost);
  2373. unsigned long flags;
  2374. shost = scsi_host_get(shost);
  2375. if (!shost) {
  2376. printk(KERN_ERR "Invalid state. Cannot notify host removal "
  2377. "of session teardown event because host already "
  2378. "removed.\n");
  2379. return;
  2380. }
  2381. spin_lock_irqsave(&ihost->lock, flags);
  2382. ihost->num_sessions--;
  2383. if (ihost->num_sessions == 0)
  2384. wake_up(&ihost->session_removal_wq);
  2385. spin_unlock_irqrestore(&ihost->lock, flags);
  2386. scsi_host_put(shost);
  2387. }
  2388. /**
  2389. * iscsi_session_setup - create iscsi cls session and host and session
  2390. * @iscsit: iscsi transport template
  2391. * @shost: scsi host
  2392. * @cmds_max: session can queue
  2393. * @cmd_task_size: LLD task private data size
  2394. * @initial_cmdsn: initial CmdSN
  2395. *
  2396. * This can be used by software iscsi_transports that allocate
  2397. * a session per scsi host.
  2398. *
  2399. * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
  2400. * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
  2401. * for nop handling and login/logout requests.
  2402. */
  2403. struct iscsi_cls_session *
  2404. iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
  2405. uint16_t cmds_max, int dd_size, int cmd_task_size,
  2406. uint32_t initial_cmdsn, unsigned int id)
  2407. {
  2408. struct iscsi_host *ihost = shost_priv(shost);
  2409. struct iscsi_session *session;
  2410. struct iscsi_cls_session *cls_session;
  2411. int cmd_i, scsi_cmds, total_cmds = cmds_max;
  2412. unsigned long flags;
  2413. spin_lock_irqsave(&ihost->lock, flags);
  2414. if (ihost->state == ISCSI_HOST_REMOVED) {
  2415. spin_unlock_irqrestore(&ihost->lock, flags);
  2416. return NULL;
  2417. }
  2418. ihost->num_sessions++;
  2419. spin_unlock_irqrestore(&ihost->lock, flags);
  2420. if (!total_cmds)
  2421. total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
  2422. /*
  2423. * The iscsi layer needs some tasks for nop handling and tmfs,
  2424. * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
  2425. * + 1 command for scsi IO.
  2426. */
  2427. if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
  2428. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  2429. "must be a power of two that is at least %d.\n",
  2430. total_cmds, ISCSI_TOTAL_CMDS_MIN);
  2431. goto dec_session_count;
  2432. }
  2433. if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
  2434. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  2435. "must be a power of 2 less than or equal to %d.\n",
  2436. cmds_max, ISCSI_TOTAL_CMDS_MAX);
  2437. total_cmds = ISCSI_TOTAL_CMDS_MAX;
  2438. }
  2439. if (!is_power_of_2(total_cmds)) {
  2440. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  2441. "must be a power of 2.\n", total_cmds);
  2442. total_cmds = rounddown_pow_of_two(total_cmds);
  2443. if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
  2444. return NULL;
  2445. printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
  2446. total_cmds);
  2447. }
  2448. scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
  2449. cls_session = iscsi_alloc_session(shost, iscsit,
  2450. sizeof(struct iscsi_session) +
  2451. dd_size);
  2452. if (!cls_session)
  2453. goto dec_session_count;
  2454. session = cls_session->dd_data;
  2455. session->cls_session = cls_session;
  2456. session->host = shost;
  2457. session->state = ISCSI_STATE_FREE;
  2458. session->fast_abort = 1;
  2459. session->tgt_reset_timeout = 30;
  2460. session->lu_reset_timeout = 15;
  2461. session->abort_timeout = 10;
  2462. session->scsi_cmds_max = scsi_cmds;
  2463. session->cmds_max = total_cmds;
  2464. session->queued_cmdsn = session->cmdsn = initial_cmdsn;
  2465. session->exp_cmdsn = initial_cmdsn + 1;
  2466. session->max_cmdsn = initial_cmdsn + 1;
  2467. session->max_r2t = 1;
  2468. session->tt = iscsit;
  2469. session->dd_data = cls_session->dd_data + sizeof(*session);
  2470. mutex_init(&session->eh_mutex);
  2471. spin_lock_init(&session->frwd_lock);
  2472. spin_lock_init(&session->back_lock);
  2473. /* initialize SCSI PDU commands pool */
  2474. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  2475. (void***)&session->cmds,
  2476. cmd_task_size + sizeof(struct iscsi_task)))
  2477. goto cmdpool_alloc_fail;
  2478. /* pre-format cmds pool with ITT */
  2479. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  2480. struct iscsi_task *task = session->cmds[cmd_i];
  2481. if (cmd_task_size)
  2482. task->dd_data = &task[1];
  2483. task->itt = cmd_i;
  2484. task->state = ISCSI_TASK_FREE;
  2485. INIT_LIST_HEAD(&task->running);
  2486. }
  2487. if (!try_module_get(iscsit->owner))
  2488. goto module_get_fail;
  2489. if (iscsi_add_session(cls_session, id))
  2490. goto cls_session_fail;
  2491. return cls_session;
  2492. cls_session_fail:
  2493. module_put(iscsit->owner);
  2494. module_get_fail:
  2495. iscsi_pool_free(&session->cmdpool);
  2496. cmdpool_alloc_fail:
  2497. iscsi_free_session(cls_session);
  2498. dec_session_count:
  2499. iscsi_host_dec_session_cnt(shost);
  2500. return NULL;
  2501. }
  2502. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  2503. /**
  2504. * iscsi_session_teardown - destroy session, host, and cls_session
  2505. * @cls_session: iscsi session
  2506. *
  2507. * The driver must have called iscsi_remove_session before
  2508. * calling this.
  2509. */
  2510. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  2511. {
  2512. struct iscsi_session *session = cls_session->dd_data;
  2513. struct module *owner = cls_session->transport->owner;
  2514. struct Scsi_Host *shost = session->host;
  2515. iscsi_pool_free(&session->cmdpool);
  2516. kfree(session->password);
  2517. kfree(session->password_in);
  2518. kfree(session->username);
  2519. kfree(session->username_in);
  2520. kfree(session->targetname);
  2521. kfree(session->targetalias);
  2522. kfree(session->initiatorname);
  2523. kfree(session->boot_root);
  2524. kfree(session->boot_nic);
  2525. kfree(session->boot_target);
  2526. kfree(session->ifacename);
  2527. kfree(session->portal_type);
  2528. kfree(session->discovery_parent_type);
  2529. iscsi_destroy_session(cls_session);
  2530. iscsi_host_dec_session_cnt(shost);
  2531. module_put(owner);
  2532. }
  2533. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  2534. /**
  2535. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  2536. * @cls_session: iscsi_cls_session
  2537. * @dd_size: private driver data size
  2538. * @conn_idx: cid
  2539. */
  2540. struct iscsi_cls_conn *
  2541. iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
  2542. uint32_t conn_idx)
  2543. {
  2544. struct iscsi_session *session = cls_session->dd_data;
  2545. struct iscsi_conn *conn;
  2546. struct iscsi_cls_conn *cls_conn;
  2547. char *data;
  2548. cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
  2549. conn_idx);
  2550. if (!cls_conn)
  2551. return NULL;
  2552. conn = cls_conn->dd_data;
  2553. memset(conn, 0, sizeof(*conn) + dd_size);
  2554. conn->dd_data = cls_conn->dd_data + sizeof(*conn);
  2555. conn->session = session;
  2556. conn->cls_conn = cls_conn;
  2557. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  2558. conn->id = conn_idx;
  2559. conn->exp_statsn = 0;
  2560. conn->tmf_state = TMF_INITIAL;
  2561. init_timer(&conn->transport_timer);
  2562. conn->transport_timer.data = (unsigned long)conn;
  2563. conn->transport_timer.function = iscsi_check_transport_timeouts;
  2564. INIT_LIST_HEAD(&conn->mgmtqueue);
  2565. INIT_LIST_HEAD(&conn->cmdqueue);
  2566. INIT_LIST_HEAD(&conn->requeue);
  2567. spin_lock_init(&conn->taskqueuelock);
  2568. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  2569. /* allocate login_task used for the login/text sequences */
  2570. spin_lock_bh(&session->frwd_lock);
  2571. if (!kfifo_out(&session->cmdpool.queue,
  2572. (void*)&conn->login_task,
  2573. sizeof(void*))) {
  2574. spin_unlock_bh(&session->frwd_lock);
  2575. goto login_task_alloc_fail;
  2576. }
  2577. spin_unlock_bh(&session->frwd_lock);
  2578. data = (char *) __get_free_pages(GFP_KERNEL,
  2579. get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
  2580. if (!data)
  2581. goto login_task_data_alloc_fail;
  2582. conn->login_task->data = conn->data = data;
  2583. init_timer(&conn->tmf_timer);
  2584. init_waitqueue_head(&conn->ehwait);
  2585. return cls_conn;
  2586. login_task_data_alloc_fail:
  2587. kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
  2588. sizeof(void*));
  2589. login_task_alloc_fail:
  2590. iscsi_destroy_conn(cls_conn);
  2591. return NULL;
  2592. }
  2593. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  2594. /**
  2595. * iscsi_conn_teardown - teardown iscsi connection
  2596. * cls_conn: iscsi class connection
  2597. *
  2598. * TODO: we may need to make this into a two step process
  2599. * like scsi-mls remove + put host
  2600. */
  2601. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  2602. {
  2603. struct iscsi_conn *conn = cls_conn->dd_data;
  2604. struct iscsi_session *session = conn->session;
  2605. del_timer_sync(&conn->transport_timer);
  2606. mutex_lock(&session->eh_mutex);
  2607. spin_lock_bh(&session->frwd_lock);
  2608. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  2609. if (session->leadconn == conn) {
  2610. /*
  2611. * leading connection? then give up on recovery.
  2612. */
  2613. session->state = ISCSI_STATE_TERMINATE;
  2614. wake_up(&conn->ehwait);
  2615. }
  2616. spin_unlock_bh(&session->frwd_lock);
  2617. /* flush queued up work because we free the connection below */
  2618. iscsi_suspend_tx(conn);
  2619. spin_lock_bh(&session->frwd_lock);
  2620. free_pages((unsigned long) conn->data,
  2621. get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
  2622. kfree(conn->persistent_address);
  2623. kfree(conn->local_ipaddr);
  2624. /* regular RX path uses back_lock */
  2625. spin_lock_bh(&session->back_lock);
  2626. kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
  2627. sizeof(void*));
  2628. spin_unlock_bh(&session->back_lock);
  2629. if (session->leadconn == conn)
  2630. session->leadconn = NULL;
  2631. spin_unlock_bh(&session->frwd_lock);
  2632. mutex_unlock(&session->eh_mutex);
  2633. iscsi_destroy_conn(cls_conn);
  2634. }
  2635. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  2636. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  2637. {
  2638. struct iscsi_conn *conn = cls_conn->dd_data;
  2639. struct iscsi_session *session = conn->session;
  2640. if (!session) {
  2641. iscsi_conn_printk(KERN_ERR, conn,
  2642. "can't start unbound connection\n");
  2643. return -EPERM;
  2644. }
  2645. if ((session->imm_data_en || !session->initial_r2t_en) &&
  2646. session->first_burst > session->max_burst) {
  2647. iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
  2648. "first_burst %d max_burst %d\n",
  2649. session->first_burst, session->max_burst);
  2650. return -EINVAL;
  2651. }
  2652. if (conn->ping_timeout && !conn->recv_timeout) {
  2653. iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
  2654. "zero. Using 5 seconds\n.");
  2655. conn->recv_timeout = 5;
  2656. }
  2657. if (conn->recv_timeout && !conn->ping_timeout) {
  2658. iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
  2659. "zero. Using 5 seconds.\n");
  2660. conn->ping_timeout = 5;
  2661. }
  2662. spin_lock_bh(&session->frwd_lock);
  2663. conn->c_stage = ISCSI_CONN_STARTED;
  2664. session->state = ISCSI_STATE_LOGGED_IN;
  2665. session->queued_cmdsn = session->cmdsn;
  2666. conn->last_recv = jiffies;
  2667. conn->last_ping = jiffies;
  2668. if (conn->recv_timeout && conn->ping_timeout)
  2669. mod_timer(&conn->transport_timer,
  2670. jiffies + (conn->recv_timeout * HZ));
  2671. switch(conn->stop_stage) {
  2672. case STOP_CONN_RECOVER:
  2673. /*
  2674. * unblock eh_abort() if it is blocked. re-try all
  2675. * commands after successful recovery
  2676. */
  2677. conn->stop_stage = 0;
  2678. conn->tmf_state = TMF_INITIAL;
  2679. session->age++;
  2680. if (session->age == 16)
  2681. session->age = 0;
  2682. break;
  2683. case STOP_CONN_TERM:
  2684. conn->stop_stage = 0;
  2685. break;
  2686. default:
  2687. break;
  2688. }
  2689. spin_unlock_bh(&session->frwd_lock);
  2690. iscsi_unblock_session(session->cls_session);
  2691. wake_up(&conn->ehwait);
  2692. return 0;
  2693. }
  2694. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  2695. static void
  2696. fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
  2697. {
  2698. struct iscsi_task *task;
  2699. int i, state;
  2700. for (i = 0; i < conn->session->cmds_max; i++) {
  2701. task = conn->session->cmds[i];
  2702. if (task->sc)
  2703. continue;
  2704. if (task->state == ISCSI_TASK_FREE)
  2705. continue;
  2706. ISCSI_DBG_SESSION(conn->session,
  2707. "failing mgmt itt 0x%x state %d\n",
  2708. task->itt, task->state);
  2709. state = ISCSI_TASK_ABRT_SESS_RECOV;
  2710. if (task->state == ISCSI_TASK_PENDING)
  2711. state = ISCSI_TASK_COMPLETED;
  2712. iscsi_complete_task(task, state);
  2713. }
  2714. }
  2715. static void iscsi_start_session_recovery(struct iscsi_session *session,
  2716. struct iscsi_conn *conn, int flag)
  2717. {
  2718. int old_stop_stage;
  2719. mutex_lock(&session->eh_mutex);
  2720. spin_lock_bh(&session->frwd_lock);
  2721. if (conn->stop_stage == STOP_CONN_TERM) {
  2722. spin_unlock_bh(&session->frwd_lock);
  2723. mutex_unlock(&session->eh_mutex);
  2724. return;
  2725. }
  2726. /*
  2727. * When this is called for the in_login state, we only want to clean
  2728. * up the login task and connection. We do not need to block and set
  2729. * the recovery state again
  2730. */
  2731. if (flag == STOP_CONN_TERM)
  2732. session->state = ISCSI_STATE_TERMINATE;
  2733. else if (conn->stop_stage != STOP_CONN_RECOVER)
  2734. session->state = ISCSI_STATE_IN_RECOVERY;
  2735. old_stop_stage = conn->stop_stage;
  2736. conn->stop_stage = flag;
  2737. spin_unlock_bh(&session->frwd_lock);
  2738. del_timer_sync(&conn->transport_timer);
  2739. iscsi_suspend_tx(conn);
  2740. spin_lock_bh(&session->frwd_lock);
  2741. conn->c_stage = ISCSI_CONN_STOPPED;
  2742. spin_unlock_bh(&session->frwd_lock);
  2743. /*
  2744. * for connection level recovery we should not calculate
  2745. * header digest. conn->hdr_size used for optimization
  2746. * in hdr_extract() and will be re-negotiated at
  2747. * set_param() time.
  2748. */
  2749. if (flag == STOP_CONN_RECOVER) {
  2750. conn->hdrdgst_en = 0;
  2751. conn->datadgst_en = 0;
  2752. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  2753. old_stop_stage != STOP_CONN_RECOVER) {
  2754. ISCSI_DBG_SESSION(session, "blocking session\n");
  2755. iscsi_block_session(session->cls_session);
  2756. }
  2757. }
  2758. /*
  2759. * flush queues.
  2760. */
  2761. spin_lock_bh(&session->frwd_lock);
  2762. fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
  2763. fail_mgmt_tasks(session, conn);
  2764. memset(&conn->tmhdr, 0, sizeof(conn->tmhdr));
  2765. spin_unlock_bh(&session->frwd_lock);
  2766. mutex_unlock(&session->eh_mutex);
  2767. }
  2768. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  2769. {
  2770. struct iscsi_conn *conn = cls_conn->dd_data;
  2771. struct iscsi_session *session = conn->session;
  2772. switch (flag) {
  2773. case STOP_CONN_RECOVER:
  2774. case STOP_CONN_TERM:
  2775. iscsi_start_session_recovery(session, conn, flag);
  2776. break;
  2777. default:
  2778. iscsi_conn_printk(KERN_ERR, conn,
  2779. "invalid stop flag %d\n", flag);
  2780. }
  2781. }
  2782. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  2783. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  2784. struct iscsi_cls_conn *cls_conn, int is_leading)
  2785. {
  2786. struct iscsi_session *session = cls_session->dd_data;
  2787. struct iscsi_conn *conn = cls_conn->dd_data;
  2788. spin_lock_bh(&session->frwd_lock);
  2789. if (is_leading)
  2790. session->leadconn = conn;
  2791. spin_unlock_bh(&session->frwd_lock);
  2792. /*
  2793. * Unblock xmitworker(), Login Phase will pass through.
  2794. */
  2795. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  2796. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  2797. return 0;
  2798. }
  2799. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  2800. int iscsi_switch_str_param(char **param, char *new_val_buf)
  2801. {
  2802. char *new_val;
  2803. if (*param) {
  2804. if (!strcmp(*param, new_val_buf))
  2805. return 0;
  2806. }
  2807. new_val = kstrdup(new_val_buf, GFP_NOIO);
  2808. if (!new_val)
  2809. return -ENOMEM;
  2810. kfree(*param);
  2811. *param = new_val;
  2812. return 0;
  2813. }
  2814. EXPORT_SYMBOL_GPL(iscsi_switch_str_param);
  2815. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  2816. enum iscsi_param param, char *buf, int buflen)
  2817. {
  2818. struct iscsi_conn *conn = cls_conn->dd_data;
  2819. struct iscsi_session *session = conn->session;
  2820. int val;
  2821. switch(param) {
  2822. case ISCSI_PARAM_FAST_ABORT:
  2823. sscanf(buf, "%d", &session->fast_abort);
  2824. break;
  2825. case ISCSI_PARAM_ABORT_TMO:
  2826. sscanf(buf, "%d", &session->abort_timeout);
  2827. break;
  2828. case ISCSI_PARAM_LU_RESET_TMO:
  2829. sscanf(buf, "%d", &session->lu_reset_timeout);
  2830. break;
  2831. case ISCSI_PARAM_TGT_RESET_TMO:
  2832. sscanf(buf, "%d", &session->tgt_reset_timeout);
  2833. break;
  2834. case ISCSI_PARAM_PING_TMO:
  2835. sscanf(buf, "%d", &conn->ping_timeout);
  2836. break;
  2837. case ISCSI_PARAM_RECV_TMO:
  2838. sscanf(buf, "%d", &conn->recv_timeout);
  2839. break;
  2840. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  2841. sscanf(buf, "%d", &conn->max_recv_dlength);
  2842. break;
  2843. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  2844. sscanf(buf, "%d", &conn->max_xmit_dlength);
  2845. break;
  2846. case ISCSI_PARAM_HDRDGST_EN:
  2847. sscanf(buf, "%d", &conn->hdrdgst_en);
  2848. break;
  2849. case ISCSI_PARAM_DATADGST_EN:
  2850. sscanf(buf, "%d", &conn->datadgst_en);
  2851. break;
  2852. case ISCSI_PARAM_INITIAL_R2T_EN:
  2853. sscanf(buf, "%d", &session->initial_r2t_en);
  2854. break;
  2855. case ISCSI_PARAM_MAX_R2T:
  2856. sscanf(buf, "%hu", &session->max_r2t);
  2857. break;
  2858. case ISCSI_PARAM_IMM_DATA_EN:
  2859. sscanf(buf, "%d", &session->imm_data_en);
  2860. break;
  2861. case ISCSI_PARAM_FIRST_BURST:
  2862. sscanf(buf, "%d", &session->first_burst);
  2863. break;
  2864. case ISCSI_PARAM_MAX_BURST:
  2865. sscanf(buf, "%d", &session->max_burst);
  2866. break;
  2867. case ISCSI_PARAM_PDU_INORDER_EN:
  2868. sscanf(buf, "%d", &session->pdu_inorder_en);
  2869. break;
  2870. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2871. sscanf(buf, "%d", &session->dataseq_inorder_en);
  2872. break;
  2873. case ISCSI_PARAM_ERL:
  2874. sscanf(buf, "%d", &session->erl);
  2875. break;
  2876. case ISCSI_PARAM_EXP_STATSN:
  2877. sscanf(buf, "%u", &conn->exp_statsn);
  2878. break;
  2879. case ISCSI_PARAM_USERNAME:
  2880. return iscsi_switch_str_param(&session->username, buf);
  2881. case ISCSI_PARAM_USERNAME_IN:
  2882. return iscsi_switch_str_param(&session->username_in, buf);
  2883. case ISCSI_PARAM_PASSWORD:
  2884. return iscsi_switch_str_param(&session->password, buf);
  2885. case ISCSI_PARAM_PASSWORD_IN:
  2886. return iscsi_switch_str_param(&session->password_in, buf);
  2887. case ISCSI_PARAM_TARGET_NAME:
  2888. return iscsi_switch_str_param(&session->targetname, buf);
  2889. case ISCSI_PARAM_TARGET_ALIAS:
  2890. return iscsi_switch_str_param(&session->targetalias, buf);
  2891. case ISCSI_PARAM_TPGT:
  2892. sscanf(buf, "%d", &session->tpgt);
  2893. break;
  2894. case ISCSI_PARAM_PERSISTENT_PORT:
  2895. sscanf(buf, "%d", &conn->persistent_port);
  2896. break;
  2897. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  2898. return iscsi_switch_str_param(&conn->persistent_address, buf);
  2899. case ISCSI_PARAM_IFACE_NAME:
  2900. return iscsi_switch_str_param(&session->ifacename, buf);
  2901. case ISCSI_PARAM_INITIATOR_NAME:
  2902. return iscsi_switch_str_param(&session->initiatorname, buf);
  2903. case ISCSI_PARAM_BOOT_ROOT:
  2904. return iscsi_switch_str_param(&session->boot_root, buf);
  2905. case ISCSI_PARAM_BOOT_NIC:
  2906. return iscsi_switch_str_param(&session->boot_nic, buf);
  2907. case ISCSI_PARAM_BOOT_TARGET:
  2908. return iscsi_switch_str_param(&session->boot_target, buf);
  2909. case ISCSI_PARAM_PORTAL_TYPE:
  2910. return iscsi_switch_str_param(&session->portal_type, buf);
  2911. case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
  2912. return iscsi_switch_str_param(&session->discovery_parent_type,
  2913. buf);
  2914. case ISCSI_PARAM_DISCOVERY_SESS:
  2915. sscanf(buf, "%d", &val);
  2916. session->discovery_sess = !!val;
  2917. break;
  2918. case ISCSI_PARAM_LOCAL_IPADDR:
  2919. return iscsi_switch_str_param(&conn->local_ipaddr, buf);
  2920. default:
  2921. return -ENOSYS;
  2922. }
  2923. return 0;
  2924. }
  2925. EXPORT_SYMBOL_GPL(iscsi_set_param);
  2926. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  2927. enum iscsi_param param, char *buf)
  2928. {
  2929. struct iscsi_session *session = cls_session->dd_data;
  2930. int len;
  2931. switch(param) {
  2932. case ISCSI_PARAM_FAST_ABORT:
  2933. len = sprintf(buf, "%d\n", session->fast_abort);
  2934. break;
  2935. case ISCSI_PARAM_ABORT_TMO:
  2936. len = sprintf(buf, "%d\n", session->abort_timeout);
  2937. break;
  2938. case ISCSI_PARAM_LU_RESET_TMO:
  2939. len = sprintf(buf, "%d\n", session->lu_reset_timeout);
  2940. break;
  2941. case ISCSI_PARAM_TGT_RESET_TMO:
  2942. len = sprintf(buf, "%d\n", session->tgt_reset_timeout);
  2943. break;
  2944. case ISCSI_PARAM_INITIAL_R2T_EN:
  2945. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  2946. break;
  2947. case ISCSI_PARAM_MAX_R2T:
  2948. len = sprintf(buf, "%hu\n", session->max_r2t);
  2949. break;
  2950. case ISCSI_PARAM_IMM_DATA_EN:
  2951. len = sprintf(buf, "%d\n", session->imm_data_en);
  2952. break;
  2953. case ISCSI_PARAM_FIRST_BURST:
  2954. len = sprintf(buf, "%u\n", session->first_burst);
  2955. break;
  2956. case ISCSI_PARAM_MAX_BURST:
  2957. len = sprintf(buf, "%u\n", session->max_burst);
  2958. break;
  2959. case ISCSI_PARAM_PDU_INORDER_EN:
  2960. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  2961. break;
  2962. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2963. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  2964. break;
  2965. case ISCSI_PARAM_DEF_TASKMGMT_TMO:
  2966. len = sprintf(buf, "%d\n", session->def_taskmgmt_tmo);
  2967. break;
  2968. case ISCSI_PARAM_ERL:
  2969. len = sprintf(buf, "%d\n", session->erl);
  2970. break;
  2971. case ISCSI_PARAM_TARGET_NAME:
  2972. len = sprintf(buf, "%s\n", session->targetname);
  2973. break;
  2974. case ISCSI_PARAM_TARGET_ALIAS:
  2975. len = sprintf(buf, "%s\n", session->targetalias);
  2976. break;
  2977. case ISCSI_PARAM_TPGT:
  2978. len = sprintf(buf, "%d\n", session->tpgt);
  2979. break;
  2980. case ISCSI_PARAM_USERNAME:
  2981. len = sprintf(buf, "%s\n", session->username);
  2982. break;
  2983. case ISCSI_PARAM_USERNAME_IN:
  2984. len = sprintf(buf, "%s\n", session->username_in);
  2985. break;
  2986. case ISCSI_PARAM_PASSWORD:
  2987. len = sprintf(buf, "%s\n", session->password);
  2988. break;
  2989. case ISCSI_PARAM_PASSWORD_IN:
  2990. len = sprintf(buf, "%s\n", session->password_in);
  2991. break;
  2992. case ISCSI_PARAM_IFACE_NAME:
  2993. len = sprintf(buf, "%s\n", session->ifacename);
  2994. break;
  2995. case ISCSI_PARAM_INITIATOR_NAME:
  2996. len = sprintf(buf, "%s\n", session->initiatorname);
  2997. break;
  2998. case ISCSI_PARAM_BOOT_ROOT:
  2999. len = sprintf(buf, "%s\n", session->boot_root);
  3000. break;
  3001. case ISCSI_PARAM_BOOT_NIC:
  3002. len = sprintf(buf, "%s\n", session->boot_nic);
  3003. break;
  3004. case ISCSI_PARAM_BOOT_TARGET:
  3005. len = sprintf(buf, "%s\n", session->boot_target);
  3006. break;
  3007. case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
  3008. len = sprintf(buf, "%u\n", session->auto_snd_tgt_disable);
  3009. break;
  3010. case ISCSI_PARAM_DISCOVERY_SESS:
  3011. len = sprintf(buf, "%u\n", session->discovery_sess);
  3012. break;
  3013. case ISCSI_PARAM_PORTAL_TYPE:
  3014. len = sprintf(buf, "%s\n", session->portal_type);
  3015. break;
  3016. case ISCSI_PARAM_CHAP_AUTH_EN:
  3017. len = sprintf(buf, "%u\n", session->chap_auth_en);
  3018. break;
  3019. case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
  3020. len = sprintf(buf, "%u\n", session->discovery_logout_en);
  3021. break;
  3022. case ISCSI_PARAM_BIDI_CHAP_EN:
  3023. len = sprintf(buf, "%u\n", session->bidi_chap_en);
  3024. break;
  3025. case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
  3026. len = sprintf(buf, "%u\n", session->discovery_auth_optional);
  3027. break;
  3028. case ISCSI_PARAM_DEF_TIME2WAIT:
  3029. len = sprintf(buf, "%d\n", session->time2wait);
  3030. break;
  3031. case ISCSI_PARAM_DEF_TIME2RETAIN:
  3032. len = sprintf(buf, "%d\n", session->time2retain);
  3033. break;
  3034. case ISCSI_PARAM_TSID:
  3035. len = sprintf(buf, "%u\n", session->tsid);
  3036. break;
  3037. case ISCSI_PARAM_ISID:
  3038. len = sprintf(buf, "%02x%02x%02x%02x%02x%02x\n",
  3039. session->isid[0], session->isid[1],
  3040. session->isid[2], session->isid[3],
  3041. session->isid[4], session->isid[5]);
  3042. break;
  3043. case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
  3044. len = sprintf(buf, "%u\n", session->discovery_parent_idx);
  3045. break;
  3046. case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
  3047. if (session->discovery_parent_type)
  3048. len = sprintf(buf, "%s\n",
  3049. session->discovery_parent_type);
  3050. else
  3051. len = sprintf(buf, "\n");
  3052. break;
  3053. default:
  3054. return -ENOSYS;
  3055. }
  3056. return len;
  3057. }
  3058. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  3059. int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
  3060. enum iscsi_param param, char *buf)
  3061. {
  3062. struct sockaddr_in6 *sin6 = NULL;
  3063. struct sockaddr_in *sin = NULL;
  3064. int len;
  3065. switch (addr->ss_family) {
  3066. case AF_INET:
  3067. sin = (struct sockaddr_in *)addr;
  3068. break;
  3069. case AF_INET6:
  3070. sin6 = (struct sockaddr_in6 *)addr;
  3071. break;
  3072. default:
  3073. return -EINVAL;
  3074. }
  3075. switch (param) {
  3076. case ISCSI_PARAM_CONN_ADDRESS:
  3077. case ISCSI_HOST_PARAM_IPADDRESS:
  3078. if (sin)
  3079. len = sprintf(buf, "%pI4\n", &sin->sin_addr.s_addr);
  3080. else
  3081. len = sprintf(buf, "%pI6\n", &sin6->sin6_addr);
  3082. break;
  3083. case ISCSI_PARAM_CONN_PORT:
  3084. case ISCSI_PARAM_LOCAL_PORT:
  3085. if (sin)
  3086. len = sprintf(buf, "%hu\n", be16_to_cpu(sin->sin_port));
  3087. else
  3088. len = sprintf(buf, "%hu\n",
  3089. be16_to_cpu(sin6->sin6_port));
  3090. break;
  3091. default:
  3092. return -EINVAL;
  3093. }
  3094. return len;
  3095. }
  3096. EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param);
  3097. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  3098. enum iscsi_param param, char *buf)
  3099. {
  3100. struct iscsi_conn *conn = cls_conn->dd_data;
  3101. int len;
  3102. switch(param) {
  3103. case ISCSI_PARAM_PING_TMO:
  3104. len = sprintf(buf, "%u\n", conn->ping_timeout);
  3105. break;
  3106. case ISCSI_PARAM_RECV_TMO:
  3107. len = sprintf(buf, "%u\n", conn->recv_timeout);
  3108. break;
  3109. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  3110. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  3111. break;
  3112. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  3113. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  3114. break;
  3115. case ISCSI_PARAM_HDRDGST_EN:
  3116. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  3117. break;
  3118. case ISCSI_PARAM_DATADGST_EN:
  3119. len = sprintf(buf, "%d\n", conn->datadgst_en);
  3120. break;
  3121. case ISCSI_PARAM_IFMARKER_EN:
  3122. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  3123. break;
  3124. case ISCSI_PARAM_OFMARKER_EN:
  3125. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  3126. break;
  3127. case ISCSI_PARAM_EXP_STATSN:
  3128. len = sprintf(buf, "%u\n", conn->exp_statsn);
  3129. break;
  3130. case ISCSI_PARAM_PERSISTENT_PORT:
  3131. len = sprintf(buf, "%d\n", conn->persistent_port);
  3132. break;
  3133. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  3134. len = sprintf(buf, "%s\n", conn->persistent_address);
  3135. break;
  3136. case ISCSI_PARAM_STATSN:
  3137. len = sprintf(buf, "%u\n", conn->statsn);
  3138. break;
  3139. case ISCSI_PARAM_MAX_SEGMENT_SIZE:
  3140. len = sprintf(buf, "%u\n", conn->max_segment_size);
  3141. break;
  3142. case ISCSI_PARAM_KEEPALIVE_TMO:
  3143. len = sprintf(buf, "%u\n", conn->keepalive_tmo);
  3144. break;
  3145. case ISCSI_PARAM_LOCAL_PORT:
  3146. len = sprintf(buf, "%u\n", conn->local_port);
  3147. break;
  3148. case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
  3149. len = sprintf(buf, "%u\n", conn->tcp_timestamp_stat);
  3150. break;
  3151. case ISCSI_PARAM_TCP_NAGLE_DISABLE:
  3152. len = sprintf(buf, "%u\n", conn->tcp_nagle_disable);
  3153. break;
  3154. case ISCSI_PARAM_TCP_WSF_DISABLE:
  3155. len = sprintf(buf, "%u\n", conn->tcp_wsf_disable);
  3156. break;
  3157. case ISCSI_PARAM_TCP_TIMER_SCALE:
  3158. len = sprintf(buf, "%u\n", conn->tcp_timer_scale);
  3159. break;
  3160. case ISCSI_PARAM_TCP_TIMESTAMP_EN:
  3161. len = sprintf(buf, "%u\n", conn->tcp_timestamp_en);
  3162. break;
  3163. case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
  3164. len = sprintf(buf, "%u\n", conn->fragment_disable);
  3165. break;
  3166. case ISCSI_PARAM_IPV4_TOS:
  3167. len = sprintf(buf, "%u\n", conn->ipv4_tos);
  3168. break;
  3169. case ISCSI_PARAM_IPV6_TC:
  3170. len = sprintf(buf, "%u\n", conn->ipv6_traffic_class);
  3171. break;
  3172. case ISCSI_PARAM_IPV6_FLOW_LABEL:
  3173. len = sprintf(buf, "%u\n", conn->ipv6_flow_label);
  3174. break;
  3175. case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
  3176. len = sprintf(buf, "%u\n", conn->is_fw_assigned_ipv6);
  3177. break;
  3178. case ISCSI_PARAM_TCP_XMIT_WSF:
  3179. len = sprintf(buf, "%u\n", conn->tcp_xmit_wsf);
  3180. break;
  3181. case ISCSI_PARAM_TCP_RECV_WSF:
  3182. len = sprintf(buf, "%u\n", conn->tcp_recv_wsf);
  3183. break;
  3184. case ISCSI_PARAM_LOCAL_IPADDR:
  3185. len = sprintf(buf, "%s\n", conn->local_ipaddr);
  3186. break;
  3187. default:
  3188. return -ENOSYS;
  3189. }
  3190. return len;
  3191. }
  3192. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  3193. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  3194. char *buf)
  3195. {
  3196. struct iscsi_host *ihost = shost_priv(shost);
  3197. int len;
  3198. switch (param) {
  3199. case ISCSI_HOST_PARAM_NETDEV_NAME:
  3200. len = sprintf(buf, "%s\n", ihost->netdev);
  3201. break;
  3202. case ISCSI_HOST_PARAM_HWADDRESS:
  3203. len = sprintf(buf, "%s\n", ihost->hwaddress);
  3204. break;
  3205. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  3206. len = sprintf(buf, "%s\n", ihost->initiatorname);
  3207. break;
  3208. default:
  3209. return -ENOSYS;
  3210. }
  3211. return len;
  3212. }
  3213. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  3214. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  3215. char *buf, int buflen)
  3216. {
  3217. struct iscsi_host *ihost = shost_priv(shost);
  3218. switch (param) {
  3219. case ISCSI_HOST_PARAM_NETDEV_NAME:
  3220. return iscsi_switch_str_param(&ihost->netdev, buf);
  3221. case ISCSI_HOST_PARAM_HWADDRESS:
  3222. return iscsi_switch_str_param(&ihost->hwaddress, buf);
  3223. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  3224. return iscsi_switch_str_param(&ihost->initiatorname, buf);
  3225. default:
  3226. return -ENOSYS;
  3227. }
  3228. return 0;
  3229. }
  3230. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  3231. MODULE_AUTHOR("Mike Christie");
  3232. MODULE_DESCRIPTION("iSCSI library functions");
  3233. MODULE_LICENSE("GPL");