zfcp_scsi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /*
  2. * This file is part of the zfcp device driver for
  3. * FCP adapters for IBM System z9 and zSeries.
  4. *
  5. * (C) Copyright IBM Corp. 2002, 2006
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_SCSI
  22. #include "zfcp_ext.h"
  23. static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
  24. static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
  25. static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
  26. static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
  27. void (*done) (struct scsi_cmnd *));
  28. static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
  29. static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *);
  30. static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
  31. static int zfcp_task_management_function(struct zfcp_unit *, u8,
  32. struct scsi_cmnd *);
  33. static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int,
  34. unsigned int, unsigned int);
  35. static struct device_attribute *zfcp_sysfs_sdev_attrs[];
  36. struct scsi_transport_template *zfcp_transport_template;
  37. struct zfcp_data zfcp_data = {
  38. .scsi_host_template = {
  39. .name = ZFCP_NAME,
  40. .proc_name = "zfcp",
  41. .slave_alloc = zfcp_scsi_slave_alloc,
  42. .slave_configure = zfcp_scsi_slave_configure,
  43. .slave_destroy = zfcp_scsi_slave_destroy,
  44. .queuecommand = zfcp_scsi_queuecommand,
  45. .eh_abort_handler = zfcp_scsi_eh_abort_handler,
  46. .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
  47. .eh_bus_reset_handler = zfcp_scsi_eh_host_reset_handler,
  48. .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
  49. .can_queue = 4096,
  50. .this_id = -1,
  51. .sg_tablesize = ZFCP_MAX_SBALES_PER_REQ,
  52. .cmd_per_lun = 1,
  53. .use_clustering = 1,
  54. .sdev_attrs = zfcp_sysfs_sdev_attrs,
  55. },
  56. .driver_version = ZFCP_VERSION,
  57. };
  58. /* Find start of Response Information in FCP response unit*/
  59. char *
  60. zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  61. {
  62. char *fcp_rsp_info_ptr;
  63. fcp_rsp_info_ptr =
  64. (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
  65. return fcp_rsp_info_ptr;
  66. }
  67. /* Find start of Sense Information in FCP response unit*/
  68. char *
  69. zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  70. {
  71. char *fcp_sns_info_ptr;
  72. fcp_sns_info_ptr =
  73. (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
  74. if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
  75. fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
  76. fcp_rsp_iu->fcp_rsp_len;
  77. return fcp_sns_info_ptr;
  78. }
  79. fcp_dl_t *
  80. zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
  81. {
  82. int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
  83. fcp_dl_t *fcp_dl_addr;
  84. fcp_dl_addr = (fcp_dl_t *)
  85. ((unsigned char *) fcp_cmd +
  86. sizeof (struct fcp_cmnd_iu) + additional_length);
  87. /*
  88. * fcp_dl_addr = start address of fcp_cmnd structure +
  89. * size of fixed part + size of dynamically sized add_dcp_cdb field
  90. * SEE FCP-2 documentation
  91. */
  92. return fcp_dl_addr;
  93. }
  94. fcp_dl_t
  95. zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
  96. {
  97. return *zfcp_get_fcp_dl_ptr(fcp_cmd);
  98. }
  99. void
  100. zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
  101. {
  102. *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
  103. }
  104. /*
  105. * note: it's a bit-or operation not an assignment
  106. * regarding the specified byte
  107. */
  108. static inline void
  109. set_byte(u32 * result, char status, char pos)
  110. {
  111. *result |= status << (pos * 8);
  112. }
  113. void
  114. set_host_byte(u32 * result, char status)
  115. {
  116. set_byte(result, status, 2);
  117. }
  118. void
  119. set_driver_byte(u32 * result, char status)
  120. {
  121. set_byte(result, status, 3);
  122. }
  123. static int
  124. zfcp_scsi_slave_alloc(struct scsi_device *sdp)
  125. {
  126. struct zfcp_adapter *adapter;
  127. struct zfcp_unit *unit;
  128. unsigned long flags;
  129. int retval = -ENXIO;
  130. adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
  131. if (!adapter)
  132. goto out;
  133. read_lock_irqsave(&zfcp_data.config_lock, flags);
  134. unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
  135. if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
  136. &unit->status)) {
  137. sdp->hostdata = unit;
  138. unit->device = sdp;
  139. zfcp_unit_get(unit);
  140. retval = 0;
  141. }
  142. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  143. out:
  144. return retval;
  145. }
  146. /**
  147. * zfcp_scsi_slave_destroy - called when scsi device is removed
  148. *
  149. * Remove reference to associated scsi device for an zfcp_unit.
  150. * Mark zfcp_unit as failed. The scsi device might be deleted via sysfs
  151. * or a scan for this device might have failed.
  152. */
  153. static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
  154. {
  155. struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
  156. if (unit) {
  157. atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
  158. sdpnt->hostdata = NULL;
  159. unit->device = NULL;
  160. zfcp_erp_unit_failed(unit);
  161. zfcp_unit_put(unit);
  162. } else {
  163. ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
  164. "address %p\n", sdpnt);
  165. }
  166. }
  167. /*
  168. * called from scsi midlayer to allow finetuning of a device.
  169. */
  170. static int
  171. zfcp_scsi_slave_configure(struct scsi_device *sdp)
  172. {
  173. if (sdp->tagged_supported)
  174. scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
  175. else
  176. scsi_adjust_queue_depth(sdp, 0, 1);
  177. return 0;
  178. }
  179. /**
  180. * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
  181. * @scpnt: pointer to struct scsi_cmnd where result is set
  182. * @result: result to be set in scpnt (e.g. DID_ERROR)
  183. */
  184. static void
  185. zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
  186. {
  187. set_host_byte(&scpnt->result, result);
  188. if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
  189. zfcp_scsi_dbf_event_result("fail", 4,
  190. (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
  191. scpnt, NULL);
  192. /* return directly */
  193. scpnt->scsi_done(scpnt);
  194. }
  195. /**
  196. * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
  197. * zfcp_scsi_command_sync
  198. * @adapter: adapter where scsi command is issued
  199. * @unit: unit to which scsi command is sent
  200. * @scpnt: scsi command to be sent
  201. * @timer: timer to be started if request is successfully initiated
  202. *
  203. * Note: In scsi_done function must be set in scpnt.
  204. */
  205. int
  206. zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
  207. struct scsi_cmnd *scpnt, struct timer_list *timer)
  208. {
  209. int tmp;
  210. int retval;
  211. retval = 0;
  212. BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
  213. BUG_ON(scpnt->scsi_done == NULL);
  214. if (unlikely(NULL == unit)) {
  215. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  216. goto out;
  217. }
  218. if (unlikely(
  219. atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
  220. !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
  221. ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
  222. "0x%016Lx on adapter %s\n",
  223. unit->fcp_lun, unit->port->wwpn,
  224. zfcp_get_busid_by_adapter(adapter));
  225. zfcp_scsi_command_fail(scpnt, DID_ERROR);
  226. goto out;
  227. }
  228. if (unlikely(
  229. !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))) {
  230. ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
  231. "on port 0x%016Lx in recovery\n",
  232. zfcp_get_busid_by_unit(unit),
  233. unit->fcp_lun, unit->port->wwpn);
  234. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  235. goto out;
  236. }
  237. tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, timer,
  238. ZFCP_REQ_AUTO_CLEANUP);
  239. if (unlikely(tmp < 0)) {
  240. ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
  241. retval = SCSI_MLQUEUE_HOST_BUSY;
  242. }
  243. out:
  244. return retval;
  245. }
  246. void
  247. zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
  248. {
  249. struct completion *wait = (struct completion *) scpnt->SCp.ptr;
  250. complete(wait);
  251. }
  252. /**
  253. * zfcp_scsi_command_sync - send a SCSI command and wait for completion
  254. * @unit: unit where command is sent to
  255. * @scpnt: scsi command to be sent
  256. * @timer: timer to be started if request is successfully initiated
  257. * Return: 0
  258. *
  259. * Errors are indicated in scpnt->result
  260. */
  261. int
  262. zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
  263. struct timer_list *timer)
  264. {
  265. int ret;
  266. DECLARE_COMPLETION(wait);
  267. scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
  268. scpnt->scsi_done = zfcp_scsi_command_sync_handler;
  269. ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt, timer);
  270. if (ret == 0)
  271. wait_for_completion(&wait);
  272. scpnt->SCp.ptr = NULL;
  273. return 0;
  274. }
  275. /*
  276. * function: zfcp_scsi_queuecommand
  277. *
  278. * purpose: enqueues a SCSI command to the specified target device
  279. *
  280. * returns: 0 - success, SCSI command enqueued
  281. * !0 - failure
  282. */
  283. int
  284. zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
  285. void (*done) (struct scsi_cmnd *))
  286. {
  287. struct zfcp_unit *unit;
  288. struct zfcp_adapter *adapter;
  289. /* reset the status for this request */
  290. scpnt->result = 0;
  291. scpnt->host_scribble = NULL;
  292. scpnt->scsi_done = done;
  293. /*
  294. * figure out adapter and target device
  295. * (stored there by zfcp_scsi_slave_alloc)
  296. */
  297. adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
  298. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  299. return zfcp_scsi_command_async(adapter, unit, scpnt, NULL);
  300. }
  301. static struct zfcp_unit *
  302. zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, unsigned int id,
  303. unsigned int lun)
  304. {
  305. struct zfcp_port *port;
  306. struct zfcp_unit *unit, *retval = NULL;
  307. list_for_each_entry(port, &adapter->port_list_head, list) {
  308. if (!port->rport || (id != port->rport->scsi_target_id))
  309. continue;
  310. list_for_each_entry(unit, &port->unit_list_head, list) {
  311. if (lun == unit->scsi_lun) {
  312. retval = unit;
  313. goto out;
  314. }
  315. }
  316. }
  317. out:
  318. return retval;
  319. }
  320. /**
  321. * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
  322. * @scpnt: pointer to scsi_cmnd to be aborted
  323. * Return: SUCCESS - command has been aborted and cleaned up in internal
  324. * bookkeeping, SCSI stack won't be called for aborted command
  325. * FAILED - otherwise
  326. *
  327. * We do not need to care for a SCSI command which completes normally
  328. * but late during this abort routine runs. We are allowed to return
  329. * late commands to the SCSI stack. It tracks the state of commands and
  330. * will handle late commands. (Usually, the normal completion of late
  331. * commands is ignored with respect to the running abort operation.)
  332. */
  333. int
  334. zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
  335. {
  336. struct Scsi_Host *scsi_host;
  337. struct zfcp_adapter *adapter;
  338. struct zfcp_unit *unit;
  339. int retval = SUCCESS;
  340. struct zfcp_fsf_req *new_fsf_req = NULL;
  341. struct zfcp_fsf_req *old_fsf_req;
  342. unsigned long flags;
  343. scsi_host = scpnt->device->host;
  344. adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
  345. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  346. ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
  347. scpnt, zfcp_get_busid_by_adapter(adapter));
  348. /* avoid race condition between late normal completion and abort */
  349. write_lock_irqsave(&adapter->abort_lock, flags);
  350. /*
  351. * Check whether command has just completed and can not be aborted.
  352. * Even if the command has just been completed late, we can access
  353. * scpnt since the SCSI stack does not release it at least until
  354. * this routine returns. (scpnt is parameter passed to this routine
  355. * and must not disappear during abort even on late completion.)
  356. */
  357. old_fsf_req = (struct zfcp_fsf_req *) scpnt->host_scribble;
  358. if (!old_fsf_req) {
  359. write_unlock_irqrestore(&adapter->abort_lock, flags);
  360. zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, NULL);
  361. retval = SUCCESS;
  362. goto out;
  363. }
  364. old_fsf_req->data = 0;
  365. old_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
  366. /* don't access old_fsf_req after releasing the abort_lock */
  367. write_unlock_irqrestore(&adapter->abort_lock, flags);
  368. /* call FSF routine which does the abort */
  369. new_fsf_req = zfcp_fsf_abort_fcp_command((unsigned long) old_fsf_req,
  370. adapter, unit, 0);
  371. if (!new_fsf_req) {
  372. ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
  373. zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
  374. old_fsf_req);
  375. retval = FAILED;
  376. goto out;
  377. }
  378. /* wait for completion of abort */
  379. __wait_event(new_fsf_req->completion_wq,
  380. new_fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  381. /* status should be valid since signals were not permitted */
  382. if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
  383. zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, new_fsf_req,
  384. NULL);
  385. retval = SUCCESS;
  386. } else if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
  387. zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, new_fsf_req,
  388. NULL);
  389. retval = SUCCESS;
  390. } else {
  391. zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, new_fsf_req,
  392. NULL);
  393. retval = FAILED;
  394. }
  395. zfcp_fsf_req_free(new_fsf_req);
  396. out:
  397. return retval;
  398. }
  399. int
  400. zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
  401. {
  402. int retval;
  403. struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
  404. if (!unit) {
  405. ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
  406. retval = SUCCESS;
  407. goto out;
  408. }
  409. ZFCP_LOG_NORMAL("resetting unit 0x%016Lx\n", unit->fcp_lun);
  410. /*
  411. * If we do not know whether the unit supports 'logical unit reset'
  412. * then try 'logical unit reset' and proceed with 'target reset'
  413. * if 'logical unit reset' fails.
  414. * If the unit is known not to support 'logical unit reset' then
  415. * skip 'logical unit reset' and try 'target reset' immediately.
  416. */
  417. if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
  418. &unit->status)) {
  419. retval = zfcp_task_management_function(unit,
  420. FCP_LOGICAL_UNIT_RESET,
  421. scpnt);
  422. if (retval) {
  423. ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
  424. if (retval == -ENOTSUPP)
  425. atomic_set_mask
  426. (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
  427. &unit->status);
  428. /* fall through and try 'target reset' next */
  429. } else {
  430. ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
  431. unit);
  432. /* avoid 'target reset' */
  433. retval = SUCCESS;
  434. goto out;
  435. }
  436. }
  437. retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
  438. if (retval) {
  439. ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
  440. retval = FAILED;
  441. } else {
  442. ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
  443. retval = SUCCESS;
  444. }
  445. out:
  446. return retval;
  447. }
  448. static int
  449. zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
  450. struct scsi_cmnd *scpnt)
  451. {
  452. struct zfcp_adapter *adapter = unit->port->adapter;
  453. struct zfcp_fsf_req *fsf_req;
  454. int retval = 0;
  455. /* issue task management function */
  456. fsf_req = zfcp_fsf_send_fcp_command_task_management
  457. (adapter, unit, tm_flags, 0);
  458. if (!fsf_req) {
  459. ZFCP_LOG_INFO("error: creation of task management request "
  460. "failed for unit 0x%016Lx on port 0x%016Lx on "
  461. "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
  462. zfcp_get_busid_by_adapter(adapter));
  463. zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
  464. retval = -ENOMEM;
  465. goto out;
  466. }
  467. __wait_event(fsf_req->completion_wq,
  468. fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  469. /*
  470. * check completion status of task management function
  471. */
  472. if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
  473. zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
  474. retval = -EIO;
  475. } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
  476. zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
  477. retval = -ENOTSUPP;
  478. } else
  479. zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
  480. zfcp_fsf_req_free(fsf_req);
  481. out:
  482. return retval;
  483. }
  484. /**
  485. * zfcp_scsi_eh_host_reset_handler - handler for host and bus reset
  486. *
  487. * If ERP is already running it will be stopped.
  488. */
  489. int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
  490. {
  491. struct zfcp_unit *unit;
  492. struct zfcp_adapter *adapter;
  493. unsigned long flags;
  494. unit = (struct zfcp_unit*) scpnt->device->hostdata;
  495. adapter = unit->port->adapter;
  496. ZFCP_LOG_NORMAL("host/bus reset because of problems with "
  497. "unit 0x%016Lx\n", unit->fcp_lun);
  498. write_lock_irqsave(&adapter->erp_lock, flags);
  499. if (atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
  500. &adapter->status)) {
  501. zfcp_erp_modify_adapter_status(adapter,
  502. ZFCP_STATUS_COMMON_UNBLOCKED|ZFCP_STATUS_COMMON_OPEN,
  503. ZFCP_CLEAR);
  504. zfcp_erp_action_dismiss_adapter(adapter);
  505. write_unlock_irqrestore(&adapter->erp_lock, flags);
  506. zfcp_fsf_req_dismiss_all(adapter);
  507. adapter->fsf_req_seq_no = 0;
  508. zfcp_erp_adapter_reopen(adapter, 0);
  509. } else {
  510. write_unlock_irqrestore(&adapter->erp_lock, flags);
  511. zfcp_erp_adapter_reopen(adapter, 0);
  512. zfcp_erp_wait(adapter);
  513. }
  514. return SUCCESS;
  515. }
  516. int
  517. zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
  518. {
  519. int retval = 0;
  520. static unsigned int unique_id = 0;
  521. /* register adapter as SCSI host with mid layer of SCSI stack */
  522. adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
  523. sizeof (struct zfcp_adapter *));
  524. if (!adapter->scsi_host) {
  525. ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
  526. "for adapter %s ",
  527. zfcp_get_busid_by_adapter(adapter));
  528. retval = -EIO;
  529. goto out;
  530. }
  531. ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
  532. /* tell the SCSI stack some characteristics of this adapter */
  533. adapter->scsi_host->max_id = 1;
  534. adapter->scsi_host->max_lun = 1;
  535. adapter->scsi_host->max_channel = 0;
  536. adapter->scsi_host->unique_id = unique_id++; /* FIXME */
  537. adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
  538. adapter->scsi_host->transportt = zfcp_transport_template;
  539. /*
  540. * save a pointer to our own adapter data structure within
  541. * hostdata field of SCSI host data structure
  542. */
  543. adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
  544. if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
  545. scsi_host_put(adapter->scsi_host);
  546. retval = -EIO;
  547. goto out;
  548. }
  549. atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  550. out:
  551. return retval;
  552. }
  553. void
  554. zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
  555. {
  556. struct Scsi_Host *shost;
  557. struct zfcp_port *port;
  558. shost = adapter->scsi_host;
  559. if (!shost)
  560. return;
  561. read_lock_irq(&zfcp_data.config_lock);
  562. list_for_each_entry(port, &adapter->port_list_head, list)
  563. if (port->rport)
  564. port->rport = NULL;
  565. read_unlock_irq(&zfcp_data.config_lock);
  566. fc_remove_host(shost);
  567. scsi_remove_host(shost);
  568. scsi_host_put(shost);
  569. adapter->scsi_host = NULL;
  570. atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  571. return;
  572. }
  573. void
  574. zfcp_fsf_start_scsi_er_timer(struct zfcp_adapter *adapter)
  575. {
  576. adapter->scsi_er_timer.function = zfcp_fsf_scsi_er_timeout_handler;
  577. adapter->scsi_er_timer.data = (unsigned long) adapter;
  578. adapter->scsi_er_timer.expires = jiffies + ZFCP_SCSI_ER_TIMEOUT;
  579. add_timer(&adapter->scsi_er_timer);
  580. }
  581. /*
  582. * Support functions for FC transport class
  583. */
  584. static struct fc_host_statistics*
  585. zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
  586. {
  587. struct fc_host_statistics *fc_stats;
  588. if (!adapter->fc_stats) {
  589. fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
  590. if (!fc_stats)
  591. return NULL;
  592. adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
  593. }
  594. memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
  595. return adapter->fc_stats;
  596. }
  597. static void
  598. zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
  599. struct fsf_qtcb_bottom_port *data,
  600. struct fsf_qtcb_bottom_port *old)
  601. {
  602. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
  603. old->seconds_since_last_reset;
  604. fc_stats->tx_frames = data->tx_frames - old->tx_frames;
  605. fc_stats->tx_words = data->tx_words - old->tx_words;
  606. fc_stats->rx_frames = data->rx_frames - old->rx_frames;
  607. fc_stats->rx_words = data->rx_words - old->rx_words;
  608. fc_stats->lip_count = data->lip - old->lip;
  609. fc_stats->nos_count = data->nos - old->nos;
  610. fc_stats->error_frames = data->error_frames - old->error_frames;
  611. fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
  612. fc_stats->link_failure_count = data->link_failure - old->link_failure;
  613. fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
  614. fc_stats->loss_of_signal_count = data->loss_of_signal -
  615. old->loss_of_signal;
  616. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
  617. old->psp_error_counts;
  618. fc_stats->invalid_tx_word_count = data->invalid_tx_words -
  619. old->invalid_tx_words;
  620. fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
  621. fc_stats->fcp_input_requests = data->input_requests -
  622. old->input_requests;
  623. fc_stats->fcp_output_requests = data->output_requests -
  624. old->output_requests;
  625. fc_stats->fcp_control_requests = data->control_requests -
  626. old->control_requests;
  627. fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
  628. fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
  629. }
  630. static void
  631. zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
  632. struct fsf_qtcb_bottom_port *data)
  633. {
  634. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
  635. fc_stats->tx_frames = data->tx_frames;
  636. fc_stats->tx_words = data->tx_words;
  637. fc_stats->rx_frames = data->rx_frames;
  638. fc_stats->rx_words = data->rx_words;
  639. fc_stats->lip_count = data->lip;
  640. fc_stats->nos_count = data->nos;
  641. fc_stats->error_frames = data->error_frames;
  642. fc_stats->dumped_frames = data->dumped_frames;
  643. fc_stats->link_failure_count = data->link_failure;
  644. fc_stats->loss_of_sync_count = data->loss_of_sync;
  645. fc_stats->loss_of_signal_count = data->loss_of_signal;
  646. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
  647. fc_stats->invalid_tx_word_count = data->invalid_tx_words;
  648. fc_stats->invalid_crc_count = data->invalid_crcs;
  649. fc_stats->fcp_input_requests = data->input_requests;
  650. fc_stats->fcp_output_requests = data->output_requests;
  651. fc_stats->fcp_control_requests = data->control_requests;
  652. fc_stats->fcp_input_megabytes = data->input_mb;
  653. fc_stats->fcp_output_megabytes = data->output_mb;
  654. }
  655. /**
  656. * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
  657. *
  658. * assumption: scsi_transport_fc synchronizes calls of
  659. * get_fc_host_stats and reset_fc_host_stats
  660. * (XXX to be checked otherwise introduce locking)
  661. */
  662. static struct fc_host_statistics *
  663. zfcp_get_fc_host_stats(struct Scsi_Host *shost)
  664. {
  665. struct zfcp_adapter *adapter;
  666. struct fc_host_statistics *fc_stats;
  667. struct fsf_qtcb_bottom_port *data;
  668. int ret;
  669. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  670. fc_stats = zfcp_init_fc_host_stats(adapter);
  671. if (!fc_stats)
  672. return NULL;
  673. data = kzalloc(sizeof(*data), GFP_KERNEL);
  674. if (!data)
  675. return NULL;
  676. ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
  677. if (ret) {
  678. kfree(data);
  679. return NULL; /* XXX return zeroed fc_stats? */
  680. }
  681. if (adapter->stats_reset &&
  682. ((jiffies/HZ - adapter->stats_reset) <
  683. data->seconds_since_last_reset)) {
  684. zfcp_adjust_fc_host_stats(fc_stats, data,
  685. adapter->stats_reset_data);
  686. } else
  687. zfcp_set_fc_host_stats(fc_stats, data);
  688. kfree(data);
  689. return fc_stats;
  690. }
  691. static void
  692. zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
  693. {
  694. struct zfcp_adapter *adapter;
  695. struct fsf_qtcb_bottom_port *data, *old_data;
  696. int ret;
  697. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  698. data = kzalloc(sizeof(*data), GFP_KERNEL);
  699. if (!data)
  700. return;
  701. ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
  702. if (ret == 0) {
  703. adapter->stats_reset = jiffies/HZ;
  704. old_data = adapter->stats_reset_data;
  705. adapter->stats_reset_data = data; /* finally freed in
  706. adater_dequeue */
  707. kfree(old_data);
  708. }
  709. }
  710. static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  711. {
  712. rport->dev_loss_tmo = timeout;
  713. }
  714. struct fc_function_template zfcp_transport_functions = {
  715. .show_starget_port_id = 1,
  716. .show_starget_port_name = 1,
  717. .show_starget_node_name = 1,
  718. .show_rport_supported_classes = 1,
  719. .show_rport_maxframe_size = 1,
  720. .show_rport_dev_loss_tmo = 1,
  721. .show_host_node_name = 1,
  722. .show_host_port_name = 1,
  723. .show_host_permanent_port_name = 1,
  724. .show_host_supported_classes = 1,
  725. .show_host_supported_speeds = 1,
  726. .show_host_maxframe_size = 1,
  727. .show_host_serial_number = 1,
  728. .get_fc_host_stats = zfcp_get_fc_host_stats,
  729. .reset_fc_host_stats = zfcp_reset_fc_host_stats,
  730. .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
  731. /* no functions registered for following dynamic attributes but
  732. directly set by LLDD */
  733. .show_host_port_type = 1,
  734. .show_host_speed = 1,
  735. .show_host_port_id = 1,
  736. };
  737. /**
  738. * ZFCP_DEFINE_SCSI_ATTR
  739. * @_name: name of show attribute
  740. * @_format: format string
  741. * @_value: value to print
  742. *
  743. * Generates attribute for a unit.
  744. */
  745. #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
  746. static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
  747. char *buf) \
  748. { \
  749. struct scsi_device *sdev; \
  750. struct zfcp_unit *unit; \
  751. \
  752. sdev = to_scsi_device(dev); \
  753. unit = sdev->hostdata; \
  754. return sprintf(buf, _format, _value); \
  755. } \
  756. \
  757. static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
  758. ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
  759. ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
  760. ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
  761. static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
  762. &dev_attr_fcp_lun,
  763. &dev_attr_wwpn,
  764. &dev_attr_hba_id,
  765. NULL
  766. };
  767. #undef ZFCP_LOG_AREA