libiscsi.c 79 KB

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