libiscsi.c 79 KB

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