libiscsi.c 97 KB

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