libiscsi.c 77 KB

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