libiscsi.c 97 KB

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